Friday, 18 July 2008

Minimalistic web server in Python

Sometimes it gets handy to have a very minimalistic web server, for sharing your favourite music with a friend or when testing webdesign layouts with various browsers on different machines.
It's very easy to write one in Python... Anyway, it's already written, you just launch it.
import sys
import BaseHTTPServer
import SimpleHTTPServer

listen = ":8080"
if len(sys.argv) > 1:
listen = sys.argv[1] + listen
listen = listen.split(":")[:2]
listen[1] = int(listen[1])
listen = tuple(listen)

httpd = BaseHTTPServer.HTTPServer(listen, SimpleHTTPServer.SimpleHTTPRequestHandler)
httpd.serve_forever()
It serves the contents of the current directory. You can give it an argument like localhost:8080, 0.0.0.0 or :8000 to listen on different IP/port. But be warned that this simple stuff is very insecure!

1 comment:

ウェブ開発者 | リチー ライアン said...

wow, i didn't know that python can also do this. now I'm really interested to get deeper knowledge on this. I'm not a linux user, but I'm interested about python. Do you think it is best to have linux OS for developing python or windows is OK, as long as you can use run the scripts?

I'm just worried if there would be some restrictions in windows that will force me to do it on Linux (maybe on ubuntu).

thanks,

python n00b