Deprecated: Assigning the return value of new by reference is deprecated in /fs5e/blendedtechnologies/public/wp-content/plugins/pmetrics.php on line 1192
Blended Technologies » Blog Archive » Python Trick: Really Simple HTTP Server

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    ('', 3727)
        if self.path=='/move':
            #This URL will trigger our sample function and send what it returns back to the browser
            self.wfile.write(move()) #call sample function here
            #serve files, and directory listings by following self.path from
            #current working directory

httpd = SocketServer.ThreadingTCPServer(('localhost', PORT),CustomHandler)

print "serving at port", PORT
httpd.serve_forever() |  Digg |  FURL |  Yahoo! My Web 2.0 |  Reddit

13 Responses to “Python Trick: Really Simple HTTP Server”

  1. Muharem Hrnjadovic Says:

    I once did a similar exercise and wrote a server (including a ‘request dispatch framework’) in 50 lines of code:

  2. jeremy Says:

    cherrypy already does this:
    it’s like the WEBrick of python.

  3. Matt Dugan Says:

    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:

    python -c “import SimpleHTTPServer;SimpleHTTPServer.test()”

    It runs a web server on the current directory on port 8000. Handy :-)

  4. Greg - CEO/Founder Says:

    Matt, that’s really clever. I really hope I remember that next time I need to quickly share a file.

  5. Kathy Says:

    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!!

  6. Sidh Says:

    A minimal HTTP Server using python:

    Create a file named
    #!/usr/bin/env bash
    python -m SimpleHTTPServer 80

    Change the permission:
    chmod 700

    And run as:
    nohup ./ &

    It runs a web server on the current directory on port 80.

  7. best site for cheap nfl jerseys Says:

    nike free take 2 sale articles

  8. シャネル財布コピー Says:

    Compared with most Shanghai organic restaurants, which simply stick to the use of organic ingredients, eateries in Hong Kong, Taipei and Tokyo take the concept much further. This includes the dining ambience, interior design, tableware and even the selection of staff, Dai says.

  9. ジョンストンズ チェック柄 カシミア 大判 ストール Says:

    bag Got You All the way down? I Have The Most Effective Solution

  10. doudoune moncler pas cher Says:

    great, required to find out this particular because i have been wanting in order to model a few stuff through dvd and also metacafe Cool, the right factors! I actually we appreciate you an individual making all these views accessible, the other products of the web-site can also be high quality. Use a enjoyment.

  11. Women's Clarks Smart Says:

    Key points from the Autumn Statement included:

  12. Donne Nike Blazer Low Says:

    Britain’s largest drugs company has admitted that its staff used a ?320 million travel budget in China to channel kickbacks towards doctors who prescribed its drugs. Some of this hospitality fund was used for legitimate purposes but other payments went to corrupt travel agents who offered cash, gifts and even procured sexual favours for physicians who chose GSK’s medicines.

  13. Python:Embedded Web Server in Python? – IT Sprite Says:

    […] I made this one. It just enhances Python’s SimpleHTTPServer a bit to let you define custom actions depending on the request. […]

Leave a Reply