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.

图片[1]-Generate QR Code in Python to transfer files from laptop to mobile - 拾光赋-拾光赋
Sergey Kislyakov

Nov 6 ’18

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

图片[2]-Generate QR Code in Python to transfer files from laptop to mobile - 拾光赋-拾光赋
After running,the QR code will pop up to your screen and the server starts running.
图片[3]-Generate QR Code in Python to transfer files from laptop to mobile - 拾光赋-拾光赋
To access your files, scan the QR code and paste that link in your mobile browser.

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

Code: GitHub

原文链接:Generate QR Code in Python to transfer files from laptop to mobile

© 版权声明
THE END
喜欢就支持一下吧
点赞7 分享
评论 抢沙发

请登录后发表评论

    暂无评论内容