File sharing is a common need in today’s digital age, and using Python to create a simple file sharing application can really help you understand the basic concepts of computer networks. This course will guide you through the process of creating a basic file sharing application using Python and several built-in applications. Third party libraries will do this. Let’s dive in!
Why Build a File Sharing App?
Understanding the principles of computer networks requires more than theoretical knowledge. Creating a hands-on project, such as a file-sharing application It will help you understand concepts such as network servers, HTTP protocols, and socket programming. This project uses the power of Python to create a simple but powerful file sharing application.
Prerequisites
Before we begin Make sure you have a basic understanding of Python programming and that Python is installed on your system. You’ll also need to install some Python packages using pip. Here’s how to get started.
Required Python Modules
- http.server and socketserver: for hosting file share servers.
- Web Browser: To display the QR code on the web.
- pyqrcode: To create QR codes for easy access.
- png: to convert QR code to PNG file.
- os: for interacting with the operating system Socket: for network connection function.
Step-by-Step Implementation
Step 1: Install the Required Packages
You will need to install the following third-party packages.
pip install pyqrcode pypng
Step 2: Import Necessary Modules
import http.server
import socket
import socketserver
import webbrowser
import pyqrcode
from pyqrcode import QRCode
import png
import os
Step 3: Assign Port and Get User Information
# Assign the port number for the server
PORT = 8010
# Get the user profile path
user_profile = os.environ['USERPROFILE']
Step 4: Change Directory to Desktop
# Change the directory to the Desktop
desktop_path = os.path.join(user_profile, 'OneDrive')
os.chdir(desktop_path)
Step 5: Create HTTP Request Handler
# Set up the HTTP request handler
Handler = http.server.SimpleHTTPRequestHandler
# Get the host name of the machine
hostname = socket.gethostname()
Step 6: Find the IP Address and Generate QR Code
# Find the IP address of the machine
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
# Generate the QR code for the IP address
url = pyqrcode.create(link)
url.svg("myqr.svg", scale=8)
# Open the generated QR code in the web browser
webbrowser.open('myqr.svg')
Step 7: Serve the Directory Over HTTP
# Create and serve the HTTP request
with socketserver.TCPServer(("", PORT), Handler) as httpd:
print(f"Serving at port {PORT}")
print(f"Type this in your browser: {IP}")
print("Or use the QR Code")
httpd.serve_forever()
Running the Code
- Save the above code in a Python file, such as ,
file_sharing.py
. - Run the file using Python. This will create a QR code and display it on your default web browser.
- Scan the QR code using your mobile device or enter the IP address displayed on your mobile browser to access the file on your desktop.
Why Use Port 8010?
Port 8010 is normally used for TCP communications. It is more secure and less susceptible to virus or Trojan attacks. This makes it a suitable choice for sharing files over a local network.
How the Code Works
- How to get user information: The code retrieves the user’s desktop path using the operating system module.
- Host and IP Address Lookup: Specify the host name and IP address of your machine to serve files on the specified port.
- QR code generation: IP addresses are converted to QR codes for easy access. which will be displayed on the web page.
- File Serving: Once the server is set up You can access your desktop files from any device on the same network by scanning a QR code or typing the IP address into your browser.
Final Thoughts
This project is a simple but powerful way to understand Python’s capabilities in networking and file sharing. Follow these steps and you should have a working file sharing application built in no time. Experiment with features like password protection or encryption. To increase the performance of your application Happy coding!