File Sharing App Using Python With Source Code
Introduction :
Computer Networks is 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 simple file-sharing app using Python. An HTTP Web Server is software that understands URLs (web address) and HTTP (the protocol used to view webpages).
Python has several packages which is a collection of modules. And it has several built- in servers. This app can support many types of file and their sizes And also provide
functionalities like encryption for secure file transfer , progress tracking and error handling . This project serves as an introduction to networking and file I/O operations in
Python .
Language And Interface :-
File Sharing App Using Python , A Versatile Programming Language With Source Code .
Required Modules Or Packages:-
1 . HTTPServer :- It is a socket server , which creates and listens at the HTTP Socket .
2 . socketserver :- These modules simplify the task of writing network servers .
3 . webbrowser :- This module provide us with a high-level interface to allow and display Web-based documents, simply calling the open() function.
4 . pyqrcode :- This module is used to generate QR Code in just two lines of code.
5 . OS :- This 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.
6 . PyPNG :- This module allows PNG image files to be read and written using pure Python
How To Run The Code :-
Step 1 . First , You Download and Install Visual Studio Code or VS Code In your PC or
Laptop by VS Code Official Website .
Step 2 . Now Open Visual Studio Code .
Step 3. Now Make The file named as main.py .
Step 4 . Now Copy And Paste The Code from the Link Given Below ⬇️
Step 5 . After pasting The code , Save This & Click On Run Button .
Step 6 . Now you will see the QR Code on the screen .
Step 7 . Either Scan this QR Code or enter the IP Address in your mobile browser shown
in Python Shell .
Step 8 . Now you will see all files of your PC’s directory .
Code Explaination :-
This Python code makes the Simple File Sharing App by Socket Server . It also generates the QR Code , which is links to the server’s address . It allows the user to easy access the files via scanning the QR Code .
Imports :
• HTTPServer :- It is a socket server , which creates and listens at the HTTP Socket.
• socketserver :- This module simplify the task of writing network servers .
• webbrowser :- This module provide us with a high-level interface to allow and display Web-based documents, simply calling the open() function.
• pyqrcode :- This module is used to generate QR Code in just two lines of code.
• OS :- This 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 :- This module allows PNG image files to be read and written using pure Python
How It Works :-
• Setting the Port : The script starts by defining the port for the server .
• Finding the User’s Desktop : The os module finds the user’s desktop by retrieving the path to the user’s OneDrive Folder and changes the current working directory to
this location .
• Setting up the server : A Handler is Created using SimpleHTTPRequestHandler , which serves the file by HTTP Server . This code is also determines the machine’s IP Address using socket module .
• Generating the QR Code : This code is also generates the QR Code , which is linked to the HTTP Server of machine . It includes the machine’s IP Address and port . This QR
Code is saves as SVG file named as ‘myqr.svg’ and opened in default web browser .
• Starting the Server : After generating the QR Code , this python script initializes the HTTP Server using socketserver .
Overall , this script simplifies the process of sharing the files of your laptop’s or PC’s OneDrive
folder over a HTTP Server .
Source Code :
Here Is Your Code Link From Where You Can Access or Copy The Code :-
main.py
# 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 document to users
import webbrowser
# to generate QR code
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
user_profile = os.environ.get('USERPROFILE')
# changing the directory to access the files on the desktop
# with the help of os module
desktop = os.path.join(user_profile, 'OneDrive')
os.chdir(desktop)
# creating an HTTP request handler
Handler = http.server.SimpleHTTPRequestHandler
# returns the hostname of the system
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)
s.close() # It's good practice to close the socket after use
# converting the IP address into the form of a QR code
# with the help of pyqrcode module
url = pyqrcode.create(IP)
# saves the QR code in the form of an svg
url.svg("myqr.svg", scale=8)
# opens the QR code image in the web browser
webbrowser.open('myqr.svg')
# creating the HTTP request and serving the folder on the specified port
with socketserver.TCPServer(("", PORT), Handler) as httpd:
print("Serving at port", PORT)
print("Type this in your Browser:", IP)
print("or use the QR code")
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 .
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 through OS 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 module pyqrcode 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.
Find More Projects
Build a Quiz Game Using HTML CSS and JavaScript Introduction Hello coders, you might have played various games, but were you aware …
Emoji Catcher Game Using HTML CSS and JavaScript Introduction Hello Coders, Welcome to another new blog. In this article we’ve made a …
Typing Challenge Using HTML CSS and JavaScript Introduction Hello friends, all you developer friends are welcome to our new project. If you …
Breakout Game Using HTML CSS and JavaScript With Source Code Introduction Hello friends, welcome to today’s new blog post. All of you …
Digital and Analog Clock using HTML CSS and JavaScript Introduction : This project is a digital clock and stopwatch system, which allows …
Coffee Shop Website using HTML, CSS & JavaScript Introduction : This project is a website for coffee house business. It uses HTML …