Quantcast
Channel: tail -f /dev/null
Viewing all articles
Browse latest Browse all 10

Serving Files Simply With Python

$
0
0

I had found this code for serving up a directory with a simple python command.

python -m SimpleHTTPServer 9914

The above command will start a simple HTTP server on port 9914 serving files out of the current directory and below.

For those looking for something just a bit more fancy the below script should do the trick while still keeping things simple.  Again, this will serve files from the current directory down.


#!/usr/bin/python

import BaseHTTPServer, SimpleHTTPServer

import os
import sys

def run(server_class=BaseHTTPServer.HTTPServer,
handler_class=SimpleHTTPServer.SimpleHTTPRequestHandler):
print 'Server version:',handler_class.server_version

port=8000

if len(sys.argv)>1:
if sys.argv[1].isdigit():
port=int(sys.argv[1])
server_address = ('', port)

httpd = server_class(server_address, handler_class)

myurl='http://localhost:'+str(server_address[1])+'/'
print 'Your Server is running on:',myurl
print 'and serving files from:',os.getcwd(),'and below.'
print 'To stop the server, type ^C.'

if 'b' in sys.argv:
print 'Trying to start webbrowser...'
import webbrowser
webbrowser.open(myurl)

httpd.serve_forever()

run()

The longer script was found here (site is in German but the code is clear).  It may be advisable to get the script code from the actual site since my code plugin seems to be messing up the formatting which is kind of important in python code.

If you would rather use WebDAV I have used the code from the pywebdav project with some decent results.

NOTE: I believe both the above python code and pywebdav require Python2.4 or greater but I have not tested that myself.


Viewing all articles
Browse latest Browse all 10

Trending Articles