A minimal HTTP server in python. It sends a JSON Hello World for GET requests, and echoes back JSON for POST requests.
python server.py 8009
Starting httpd on port 8009...
curl http://localhost:8009
{"received": "ok", "hello": "world"}
curl --data "{\"this\":\"is a test\"}" --header "Content-Type: application/json" http://localhost:8009
{"this": "is a test", "received": "ok"}
Adapted from this Gist, with the addition of code for reading the request body taken from this article.
Please be careful when using a server like this on production environments, because it lacks many important features (threading to name one). You can consult the python documentation about BaseHTTPServer to learn something useful to improve it.
If you are on Ubuntu, you can install this code as a service with an init script (hopefully, with some modifications that make it actually do something useful). Just modify the include server.conf
to suit your needs (possibly renaming it and redirecting output to some log files instead of /dev/null
), and copy it into /etc/init/
. You can then start/stop/restart the server with the usual service
command:
sudo service server start
@saiprasad2210 : You get access to path as
self.path
inside handler. You can process it as you like. An example is here : https://github.com/gitvipin/sql30/blob/master/sql30/api.py#L45