Python Trick: Really Simple HTTP Server
I needed a little web server running to simply serve up some HTML files, and also trigger a couple function calls via GET requests.
Here’s the script I came up with. I think it would be useful if you just need a simple HTTP server for something and don’t want to install Apache or anything crazy like that.
Oh yeah, and another bonus with this little guy is that it’s supposed to be multi-threaded.
"""
Serves files out of its current directory.
Doesn't handle POST requests.
"""
import SocketServer
import SimpleHTTPServer
PORT = 8080
def move():
""" sample function to be called via a URL"""
return 'hi'
class CustomHandler(SimpleHTTPServer.SimpleHTTPRequestHandler):
def do_GET(self):
#Sample values in self for URL: http://localhost:8080/jsxmlrpc-0.3/
#self.path '/jsxmlrpc-0.3/'
#self.raw_requestline 'GET /jsxmlrpc-0.3/ HTTP/1.1rn'
#self.client_address ('127.0.0.1', 3727)
if self.path=='/move':
#This URL will trigger our sample function and send what it returns back to the browser
self.send_response(200)
self.send_header('Content-type','text/html')
self.end_headers()
self.wfile.write(move()) #call sample function here
return
else:
#serve files, and directory listings by following self.path from
#current working directory
SimpleHTTPServer.SimpleHTTPRequestHandler.do_GET(self)
httpd = SocketServer.ThreadingTCPServer(('localhost', PORT),CustomHandler)
print "serving at port", PORT
httpd.serve_forever()






August 7th, 2008 at 2:26 am
I once did a similar exercise and wrote a server (including a ‘request dispatch framework’) in 50 lines of code: http://muharem.wordpress.com/2007/05/29/roll-your-own-server-in-50-lines-of-code/
August 7th, 2008 at 2:28 am
cherrypy already does this: http://www.cherrypy.org/
it’s like the WEBrick of python.
August 14th, 2008 at 8:08 pm
This isn’t nearly as feature rich as yours, but I have been using it for years. Typically, I run this when I just want to quickly share a directory to someone on the network. I make this script executable called wsh (web server here) and put it on my local path:
#!/bin/sh
python -c “import SimpleHTTPServer;SimpleHTTPServer.test()”
It runs a web server on the current directory on port 8000. Handy
August 14th, 2008 at 8:34 pm
Matt, that’s really clever. I really hope I remember that next time I need to quickly share a file.
January 23rd, 2009 at 11:18 am
I was just talking with a co-worker about how I needed a bar code scanner like (Albertson’s ) use to use when shopping for groceries. It would tally your grocery list, add the tax and let you know how much you have spent. I just need to be able to print a receipt when I get home or download to my budget spreadsheet.
This is so great it’s exactly what I need. Signing into Ebay and looking for a scanner now.
Great Job!!
December 16th, 2009 at 3:13 pm
A minimal HTTP Server using python:
Create a file named pws.sh:
#!/usr/bin/env bash
python -m SimpleHTTPServer 80
Change the permission:
chmod 700 pws.sh
And run as:
nohup ./pws.sh &
It runs a web server on the current directory on port 80.