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
socketserverpackage, 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.

Check out here how the file sharing app works live
Here’s a point-wise breakdown of the code:
- Import Necessary Modules:
http.server: For implementing HTTP web servers.socket: Provides access to networking capabilities.socketserver: A framework for creating network servers.webbrowser: To open web-based documents in the browser.pyqrcodeandQRCode: For generating QR codes.png: For image handling (not directly used).os: To access operating system functionality.
- Set Port Value:
PORT = 8010: Defines the port number for the server.
- Get User Profile Path:
os.environ['USERPROFILE']: Retrieves the current user’s profile path.
- Change Directory to Desktop:
desktop = os.path.join(...): Constructs the path to the OneDrive folder.os.chdir(desktop): Changes the working directory to the desktop.
- Create HTTP Request Handler:
Handler = http.server.SimpleHTTPRequestHandler: Sets up the request handler for the server.
- Get Hostname:
hostname = socket.gethostname(): Retrieves the hostname of the machine.
- Find IP Address:
s = socket.socket(...): Creates a UDP socket.s.connect(("8.8.8.8", 80)): Connects to an external IP to determine the local IP.IP = "http://" + s.getsockname()[0] + ":" + str(PORT): Constructs the server’s URL.
- Generate QR Code:
url = pyqrcode.create(link): Creates a QR code from the IP link.url.svg("myqr.svg", scale=8): Saves the QR code as an SVG file.webbrowser.open('myqr.svg'): Opens the QR code image in a web browser.
- Create and Run HTTP Server:
with socketserver.TCPServer(("", PORT), Handler) as httpd:: Sets up the TCP server.print("serving at port", PORT): Displays the server port.print("Type this in your Browser", IP): Instructs how to access the server via a browser.print("or Use the QRCode"): Indicates the QR code can be used for access.httpd.serve_forever(): Starts the server to listen for incoming requests indefinitely.
Why Use Port 8010?
TCP Port 8010 employs a defined protocol for communication based on the specific application in use. A protocol consists of standardized rules that dictate how data is transmitted over a network, ensuring secure communication that is resistant to viruses and Trojans.
Explanation:
The code retrieves the USERPROFILE name using the OS module and changes the directory to access files on the desktop. It then identifies the hostname to serve files through a designated port for secure sharing. Next, the system’s IP address is obtained to facilitate connections with specific devices. This IP address is converted into a QR code using the pyqrcode module for easy access. The generated QR code image is then hosted in a web browser. Devices connected to the same network can access the system’s files by either scanning the QR code or entering the IP address directly.





Leave a Reply