File Sharing App using Python


Bluetooth and WhatsApp are often used to send files from one device to another. Although both these methods are quite convenient, having a simple QR Code to tap into other devices’ documents is satisfyingly fun. Especially, when this can be done in minutes using Python.

With the help of Python combined with the concepts of computer networks, we can make a simple python application to share documents over different devices. This application will provide both an IP address as well as a QR Code. As required, you can scan the code or type in the IP address in the device of your choice and the host will provide access to all its documents. This application makes use of various Python modules like HTTPServer, socketserver, webbrowser, pyqrcode, OS module and PyPNG. These modules help in working with various ports, sockets, URLs and web page protocols.

Example

In this example, we will use the TCP Port 8010 to set a communication link between two devices. This is done by first setting the directory whose files we wish to share, followed by finding the IP address of the system. Further, we will change this IP address into a QR Code, which will then be hosted in a web browser. Then, we will scan the code with the device of our choice, a phone in this case and therefore, access all the files of the system from our phone.

These are the modules and libraries that we will be using in our code −

  • Http.server − For any kind of transfer to take place over a web browser, we need a HTTP socket. The Http.server module helps in creating this socket and listening to the content delivered to this socket.

  • Socket and socketserver − These modules are used to create and manage network connections. The socket module provides access to the network sockets present on an operating system whereas the socketserver module helps in creating network servers.

  • Webbrowser − This module helps in designing a simple web page view of all the documents.

  • Pyqrcode − This module helps in creating a QR Code.

  • Png − Since we are using a QR Code that will be displayed on the browser as an image, a png module will help in reading and writing the image file using Python.

  • OS − The OS module helps in interacting with the OS and allows working with files, directories, environment variables, path and processes, etc.

Moreover, we use the TCP Port 8010 because it uses a predefined protocol that helps in communication over various types of applications.

Algorithm

  • Step 1 − Import all the required libraries and modules.

  • Step 2 − Assign the port number and name of the system.

  • Step 3 − Change the path, as required.

  • Step 4 − Create a handler to serve files from the specified directory.

  • Step 5 − Create http request.

  • Step 6 − Find the IP address of the PC and convert it to a QR Code using pyqrcode module.

  • Step 7 − Display the QR Code in the browser.

  • Step 8 − Create http request to serve folders between the two devices.

#import modules 

#to work with HTTP Web servers
import http.server

#to access BSD socket interface
import socket

#to work with network servers
import socketserver

#to display the documents to user on other device as a web page
import webbrowser

#to generate a qr code
import pyqrcode
from pyqrcode import QRCode

#to convert the code in png format
import png

#to access all directories and os
import os

#assign port
PORT = 8010

#to find the name of user’s system
os.environ['USERPROFILE']

#changing the path 
desktop = os.path.join(os.path.join(os.environ['USERPROFILE']))
                      
#handler to serve files from appropriate directory
class DesktopHandler(http.server.SimpleHTTPRequestHandler):
   def translate_path(self, path):
      #to get the absolute path
      root_dir = os.path.abspath(desktop)
      #to join the root directory and the given path
      path = os.path.normpath(path)
      words = path.split('/')
      words = filter(None, words)
      path = root_dir
      for word in words:
         if os.path.dirname(word) or word in (os.curdir, os.pardir):
            continue
         path = os.path.join(path, word)
      return path


#create http request
Handler = DesktopHandler
hostname = socket.gethostname()


#to find the IP address of 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

#convert the IP address into a Qrcode using pyqrcode
url = pyqrcode.create(link)
url.svg("myqr.svg", scale=8)
webbrowser.open('myqr.svg')

#create http request and serve folders 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

serving at port 8010
Type this in your Browser http://192.168.30:178:8010
or Use the QRCode

On Scanning the QR Code, you will see that all the files in the system are now accessible on the other device. Here is the screenshot.

You can see that we can now access all the files of the directory we chose.

Conclusion

The easiest method of creating a file sharing application using Python is to make use of a QR Code. The only drawback of this method is that you might encounter errors while setting the directory path or opening the port for communication as it can be opened only once. However, given the time it takes, the approach is simpler even than WhatsApp web. Other such applications include the use of different codes on client and server machines to set up connections, thereby eliminating the use of a phone.

Updated on: 23-Aug-2023

494 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements