Archive

Posts Tagged ‘http’

pyhttpd - a tiny HTTP deamon written in Python

February 11th, 2008

It’s for my Python Course. It provides only very basic HTTP, but yet can be used for simple WWW hosting. Source.

Features:

  • GET and POST methods
  • New thread for new connection
  • Directory listing
  • Error pages
  • Document index
  • Deafult type and charset
  • Python scripts ;)
  • Configuration file
  • … and my own modules to handle these

TODOs:

  • Logging
  • Much more stability (exceptions handling, etc.)
  • Sending gzipped or chunked data (if accepted)
  • Range headers
  • … and many, many other

To run just edit pyhttpd.conf and change Port and Document root. Then type “python pyhttpd.py” and it should be now listening for connections.

How to run a python script and its output send to client? Pyhttpd checks if the requesting file is a python one. If so then it opens a python interpreter by a pipe, sends _GET and _POST dictionaries and file contents. And finally, sends interpreter outputs to client.

_GET and_POST are just dictionaries with variable names as keys. So if there is a foobar.py?i=500 request then _GET will be { ‘i’ : ‘500′ } (note that this is always a string value).

It’s GPL if someone asked.

Computer science and math, English , ,