pythonpython-3.xsocketserversimplehttpserver

How can I host the folder's content for anyone to access and have my device act as a server while it's running?


I'm trying to share a folder for anyone to access while running the code by making my local machine act as a server

PORT = 8000
DIRECTORY = "/content/sample_data"
class Handler(http.server.SimpleHTTPRequestHandler):
    def __init__(self, *args, **kwargs):
        super().__init__(*args, directory=DIRECTORY, **kwargs)
with socketserver.TCPServer(("", PORT), Handler) as httpd:
    print("serving at port", PORT)
    httpd.serve_forever()

Solution

  • Open a terminal, move to the directory that you want to give access to and run:

    python -m http.server 8000
    

    You will have a local server running on the port 8000.

    If you don't have a public ip address you can use ngrok to get one:

    Install ngrok, open another terminal and run

    ngrok http 8000
    

    Now anyone can access your folder by requesting the ngrok endpoint.