[CentralOH] Python One-Line web server

Eric Floehr eric at intellovations.com
Thu Mar 18 23:50:51 CET 2010


Saw this on "Top Ten One-Liners From CommandLineFu Explained":

http://www.catonmat.net/blog/top-ten-one-liners-from-commandlinefu-explained/

If you want a simple web server to serve up the documents in a particular
directory, for testing or whatever, you can simply do:

# python -m SimpleHTTPServer

It will attach to port 80 on all interfaces (not just localhost).  You can
specify the port by appending the port like:

# python -m SimpleHTTPServer 8080


You can do this yourself so long as your module is on the PYTHONPATH and
your module does something when run in that manner.  You can do that by
having the following in your file:

if __name__ == '__main__':
    ...stuff to do (oftentimes it's call a function)...

The special variable __name__ is usually set to the name of the module (i.e.
"SimpleHTTPServer").  However, when started from the command line, the
module works as if it was imported (all initialization code runs, etc.) but
the __name__ variable is set to "__main__".

If the PYTHONPATH is not set, it is generally the current directory and the
standard python library directory (like /usr/lib/python2.6).  Also, if you
want to execute a python file in the current directory, you don't need the
"-m".  Without the -m, the python interpreter will execute a file, not a
module.

Best Regards,
Eric
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/mailman/private/centraloh/attachments/20100318/8fa415bd/attachment.html>


More information about the CentralOH mailing list