Serving files using Http server in Python | extrovert.dev -->

Serving files using Http server in Python

Serving files using Http server in Python
Sunday, January 27, 2019

In this post , we will send the files to a user using python and ngrok. This method is just for showcasing the way that python server works. I don't recommend you to make this as a habit. I will explain the secuirity issues later in this post.


Let us import the libraries first

For python 2.X use

import SimpleHTTPServer
import SocketServer
For python 3.X use

import http.server
import socketserver

The Socketserver supplies necessary classes and funtions to run your server. Now we will set a port for it.

port=2000

Next we allow the handler to serve the files in the current directory.

handler = http.server.SimpleHTTPRequestHandler

Then we go for setting up a tcp server  and serve it forever without handling a keyboard interruption.

httpd = socketserver.TCPServer(("", PORT), handler)
print("Server started at", PORT)
httpd.serve_forever()

Now head over to https://localhost:2000 in your browser to have a glance at that files.

Now we will use ngrok to serve this files over internet. I told you that this method serves some secuirity issues. When you serve your directory over internet , it would create problem , when ngrok url is accessed by wrong people there is a chance of getting your personal data. So do it on your risk.

 Now start ngrok service using

./ngrok http 2000

You will get a url hosting your directory. If you are new to ngrok or dont posses ngrok , then get it @ https://ngrok.com

0 Response to Serving files using Http server in Python

Comments are personally moderated by our team. Promotions are not encouraged.

Post a Comment