DEV Community

Sahil Rajput
Sahil Rajput

Posted on • Updated on

Generate QR Code in Python to transfer files from laptop to mobile

In my previous article I showed how you can transfer files from laptop to mobile with just a single command.

Inspired from the below comment. I decided to create a Python program which will generate a QR code and using that QR code you can access your laptop files in your mobile.

I saw an app that generates the QR code to access the file over LAN. I don't remember the name, but I think it's not that hard to write your own. That would be much simpler to get the file rather than typing 192.168.whatever in your browser and then finding the file.


#Import libraries
import socket 
import http.server
import socketserver 
import cv2  
import pyqrcode
from pyqrcode import QRCode
from PIL import Image
Enter fullscreen mode Exit fullscreen mode

Get IP address

hostname = socket.gethostname()    
IP = socket.gethostbyname(hostname)      
print("Your Computer IP Address is:" + IP)
Enter fullscreen mode Exit fullscreen mode

url1 is a variable which will concatenate the IP ADDRESS and PORT number to form a string which will converted into a QR code and after that the http.server will start running.

url1 = IP + ":" + "8000"

#Generate QR Code
url = pyqrcode.create(url1)
url.png("myqr.png",scale=8)
img = cv2.imread("myqr.png")
#print(type(img))
cv2.imwrite('myqr.png',img)
im = Image.open('myqr.png')
im.show()

#Start http server
Handler = http.server.SimpleHTTPRequestHandler
with socketserver.TCPServer(("", 8000), Handler) as httpd:
    print("Running your port")
    httpd.serve_forever()
Enter fullscreen mode Exit fullscreen mode

Now, save that file with name test.py and run.

python test.py
Enter fullscreen mode Exit fullscreen mode

screenshot 2018-11-06 at 11 34 52 pm
After running,the QR code will pop up to your screen and the server starts running.
screenshot 2018-11-06 at 11 34 59 pm
To access your files, scan the QR code and paste that link in your mobile browser.
screenshot 2018-11-06 at 11 36 25 pm

NOTE: Your laptop and your mobile should be to same network

Code: GitHub

Top comments (1)

Collapse
 
ronnieicpd profile image
Ronnie

Nice article WhatsApp Status Video