Computer Networks is a vital topic in computer science, and grasping its concepts often requires practical application. In this article, we will guide you through the process of creating a simple file-sharing application using Python. This project will not only reinforce your understanding of networking principles but also provide hands-on experience with relevant Python modules.
What is an HTTP Web Server?
An HTTP Web Server is software that understands URLs (web addresses) and the HTTP protocol, which is the foundation for viewing webpages. Python offers several packages that include a collection of modules to facilitate network programming, as well as built-in servers that simplify the development of web applications.
Key Modules Used in This Project
HTTPServer: This module, part of the socketserver package, creates and listens on the HTTP socket. It is essential for handling HTTP requests.
Socketserver: This module simplifies the task of writing network servers, making it easier to create custom server functionalities.
Webbrowser: This high-level interface allows us to open web-based documents easily by calling the open() function.
PyQRCode: This module enables us to generate QR codes with just a couple of lines of code, providing a quick way to share information.
OS Module: This module facilitates interaction with the operating system. It is used for opening files, manipulating paths, and reading files directly from the command line.
PyPNG: This library allows for the reading and writing of PNG image files using pure Python, which is useful for handling QR codes in image format.
Step-by-Step Approach
Here’s a detailed outline of the steps involved in creating the file-sharing app:
Install Required Modules: Begin by installing the necessary third-party modules via the command line:
pip install pyqrcode
pip install pypng
Import Necessary Modules: Import the following modules at the beginning of your Python script:
`# import necessary modules
for implementing the HTTP Web servers
import http.server
provides access to the BSD socket interface
import socket
a framework for network servers
import socketserver
to display a Web-based documents to users
import webbrowser
to generate qrcode
import pyqrcode
from pyqrcode import QRCode
convert into png format
import png
to access operating system control
import os
assigning the appropriate port value
PORT = 8010
this finds the name of the computer user
os.environ['USERPROFILE']
changing the directory to access the files desktop
with the help of os module
desktop = os.path.join(os.path.join(os.environ['USERPROFILE']),
'OneDrive')
os.chdir(desktop)
creating a http request
Handler = http.server.SimpleHTTPRequestHandler
returns, host name of the system under
which Python interpreter is executed
hostname = socket.gethostname()
finding the IP address of the PC
s = 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 Qrcode
url = pyqrcode.create(link)
saves the Qrcode inform of svg
url.svg("myqr.svg", scale=8)
opens the Qrcode image in the web browser
webbrowser.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 server
with socketserver.TCPServer(("", PORT), Handler) as httpd:
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.
Top comments (1)
check out the whole tutorial here