If you google “single line python server” you can find many references to SimpleHttpServer implementation of python. This is really useful for testing a remote http server in your local machine by changing output of the service as you wish.
It’s usage is really simple, so you can start the web server by typing the following command :
> python -m SimpleHTTPServer 8000
Now you have a web server on your local machine listening on port 8000. You can test it by using the url below in your browser :
> http://127.0.0.1:8000
The directory that you run the command is the root of your web server. You can put a simple json file into this directory and return it against your http “get” calls by adding this json file into your url :
> http://127.0.0.1:8000/result.json
The restriction of this simple http server is that you can use only “get” method for your http calls. If you try to send post requests, you will get the following or any similar error on the console.
> code 501, message Unsupported method ('POST')
Let’s add “post” method support to the SimpleHttpServer. The following python script inherits SimpleHttpServer and adds do_POST method for simulating post http calls.
#!/usr/bin/python
import SimpleHTTPServer
import SocketServer
import logging
import cgi
import sys
class PostServerHandler(SimpleHTTPServer.SimpleHTTPRequestHandler):
def do_GET(self):
logging.warning("GET CALL")
SimpleHTTPServer.SimpleHTTPRequestHandler.do_GET(self)
def do_POST(self):
logging.warning("POST CALL")
form = cgi.FieldStorage(
fp=self.rfile,
headers=self.headers,
environ={'REQUEST_METHOD':'POST',
'CONTENT_TYPE':self.headers['Content-Type'],
})
SimpleHTTPServer.SimpleHTTPRequestHandler.do_GET(self)
Handler = PostServerHandler
httpd = SocketServer.TCPServer(("", 8000), Handler)
httpd.serve_forever()
Save the above python code into a python file like “postServer.py” and start your server with the following command :
> python postServer.py
Now you can send http post requests to your local web server.