DEV Community

Discussion on: Transfer files between your phone and computer without any cable and internet!

Collapse
 
brandonwallace profile image
brandon_wallace

Hey Chinmay,
I turned this idea into a small program. It gets the IP address automatically. Run it like this

$ python3 server_files.py

OR like this

$ python3 server_files.py --host '192.168.12.34' --port 1234

#!/usr/bin/env python3
'''
Server files from current directory.
Requirements: Python3.4+
'''

import socket
import argparse
import http.server
import socketserver


parser = argparse.ArgumentParser(description='Serve files from the current \
                                 directory.')

host_name = socket.gethostname()
ip = socket.gethostbyname(host_name)

parser.add_argument('--host', default=ip, type=str, required=False,
                    help='Specify the ip address to listen on.')

parser.add_argument('--port', default=8080, type=int, required=False,
                    help='Specify the port to listen on.')

args = parser.parse_args()

handler = http.server.SimpleHTTPRequestHandler


with socketserver.TCPServer((args.host, args.port), handler) as httpd:
    print(f'Server is listening on {args.host} on port {args.port}.')
    httpd.serve_forever()
Enter fullscreen mode Exit fullscreen mode

github.com/brandon-wallace/serve_f...