0

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()
Myelin
  • 3
  • 1

1 Answers1

0

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.

Kkameleon
  • 163
  • 2
  • 14
  • Thank You ! @Kkameleon but I need it as a python script with an input Directory path and output shareable URL (how can I get the output URL ) and am not sure if this code (code in the question ) gives result cause I cannot test if the folder is shared or not because I cannot get the URL it just still running – Myelin Oct 29 '22 at 00:41
  • The local url is http://localhost:8000 but it won't be accessible publicly. You need to use a service that will provide you a public ip address to do what you want, if you don't have one already. The best solution for the problem you describe is what I wrote above in my answer. You can call the bash commands in a python script if you want. – Kkameleon Nov 01 '22 at 17:13