Movatterモバイル変換


[0]ホーム

URL:


Open In App
Next Article:
Send message to Telegram user using Python
Next article icon

Computer Networksis an important topic and to understand the concepts, practical application of the concepts is needed. In this particular article, we will see how to make a simplefile-sharing app using Python.  AnHTTP Web Server is software that understandsURLs(web address) andHTTP(the protocol used to view webpages). Python has several packages which is a collection of modules. And it has several built-in servers. The modules used in this project are:

  • TheHTTPServeris asocketserver, which creates and listens at the HTTP socket.
  • Thesocketserver modules simplify the task of writing network servers.
  • Thewebbrowser module provides us with a high-level interface to allow and display Web-based documents, simply calling the open() function.
  • Thepyqrcode module is used to generateQR Code in just two lines of code.
  • OS module helps for interacting with the operating system. Used for opening files, manipulate paths, and read all lines in all the files on the command line.
  • PyPNG allows PNG image files to be read and written using pure Python

Step-by-step Approach:

  • Install third-party modules:
pip install pyqrcodepip install pypng
  • Install the dependencies using pip install at the command line.
  • Importing necessary modules:
    • http.server and socketserver: To host in the browser.
    • pyqrcode: To generate QRcode.
    • png: To convert the QRCode into a png file.
    • OS: To interact with the Operating system.
  • Assign port and name of the user.
  • Find Ip address of the PC and convert it to a QR code.
  • Create the HTTP request.
  • Display the QR code in browser.

Implementation of the above Approach:

Python3
# import necessary modules# for implementing the HTTP Web serversimporthttp.server# provides access to the BSD socket interfaceimportsocket# a framework for network serversimportsocketserver# to display a Web-based documents to usersimportwebbrowser# to generate qrcodeimportpyqrcodefrompyqrcodeimportQRCode# convert into png formatimportpng# to access operating system controlimportos# assigning the appropriate port valuePORT=8010# this finds the name of the computer useros.environ['USERPROFILE']# changing the directory to access the files desktop# with the help of os moduledesktop=os.path.join(os.path.join(os.environ['USERPROFILE']),'OneDrive')os.chdir(desktop)# creating a http requestHandler=http.server.SimpleHTTPRequestHandler# returns, host name of the system under# which Python interpreter is executedhostname=socket.gethostname()# finding the IP address of the PCs=socket.socket(socket.AF_INET,socket.SOCK_DGRAM)s.connect(("8.8.8.8",80))IP="http://"+s.getsockname()[0]+":"+str(PORT)link=IP# converting the IP address into the form of a QRcode# with the help of pyqrcode module# converts the IP address into a Qrcodeurl=pyqrcode.create(link)# saves the Qrcode inform of svgurl.svg("myqr.svg",scale=8)# opens the Qrcode image in the web browserwebbrowser.open('myqr.svg')# Creating the HTTP request and  serving the# folder in the PORT 8010,and the pyqrcode is generated# continuous stream of data between client and serverwithsocketserver.TCPServer(("",PORT),Handler)ashttpd:print("serving at port",PORT)print("Type this in your Browser",IP)print("or Use the QRCode")httpd.serve_forever()

Output:

  • Open the python file which has the above code on PC.
  • This will generate a QR-code.
  • Either Scan the QR-code or type the IP Address shown in the python shell in your mobile browser.
  • Share the files with ease by scanning the QR-code that's generated and get access to the files in PC, from the mobile browser.

Demonstration:

Why Port 8010 ? 

TCP Port 8010 use a defined protocol to communicate depending on the application. A protocol is a set of formalized rules that explains how data is communicated over a network. This is secured and is not infected by Virus/Trojan.

Explanation:

  • The code finds the name of the USERPROFILE throughOS module. And changes the directory to access the files on the desktop.
  • Finds the hostname to serve the file in a particular port for secured sharing.
  • Then finds the IP address of the system so that we can connect a particular device.
  • The IP address is converted into the form of QR-code using the modulepyqrcode for easy use.
  • The generated image is hosted in a web browser.
  • Once the device connected to the same network either scanned QR code or type the IP address can access the files of the system.

File Sharing App using Python
Improve

Similar Reads

We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood ourCookie Policy &Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences

[8]ページ先頭

©2009-2025 Movatter.jp