Serve.py: Difference between revisions
No edit summary |
No edit summary |
||
(2 intermediate revisions by the same user not shown) | |||
Line 1: | Line 1: | ||
== | In a time when the terms of "sharing" become increasingly contested and dictated by platforms vying for our personal data and attention, this simple one-liner, which shares the current directory (and potentially sub-directories) on one's local network takes on a political dimension. I share therefore I am! | ||
== Basic == | |||
<source lang="python"> | <source lang="python"> | ||
python -m SimpleHTTPServer | python -m SimpleHTTPServer | ||
</source> | </source> | ||
With python 3, this would become... | |||
<source lang="python"> | <source lang="python"> | ||
python -m http.server 8888 & | python -m http.server 8888 & | ||
Line 13: | Line 13: | ||
== Server with CGI support == | == Server with CGI support == | ||
To serve with [[CGI]] (just place CGI scripts in a folder named cgi-bin). | |||
<source lang="bash"> | |||
python -m CGIHTTPServer | |||
</source> | </source> | ||
Line 36: | Line 22: | ||
<source lang="python"> | <source lang="python"> | ||
#!/usr/bin/env python | #!/usr/bin/env python | ||
import BaseHTTPServer, CGIHTTPServer | import BaseHTTPServer, CGIHTTPServer | ||
import cgitb; cgitb.enable() ## This line enables CGI error reporting | import cgitb; cgitb.enable() ## This line enables CGI error reporting | ||
import sys, argparse, socket | import sys, argparse, socket | ||
from time import sleep | from time import sleep | ||
parser = argparse.ArgumentParser(description='Happy to serve you') | parser = argparse.ArgumentParser(description='Happy to serve you') | ||
parser.add_argument('--port', type=int, default=8000, help='the port number to listen to') | parser.add_argument('--port', type=int, default=8000, help='the port number to listen to') | ||
Line 46: | Line 33: | ||
parser.add_argument('--share', default=False, action="store_true", help='Run as server accessible via your local network') | parser.add_argument('--share', default=False, action="store_true", help='Run as server accessible via your local network') | ||
args = parser.parse_args() | args = parser.parse_args() | ||
server = BaseHTTPServer.HTTPServer | server = BaseHTTPServer.HTTPServer | ||
handler = CGIHTTPServer.CGIHTTPRequestHandler | handler = CGIHTTPServer.CGIHTTPRequestHandler | ||
handler.cgi_directories = ["/cgi-bin"] | handler.cgi_directories = ["/cgi-bin"] | ||
tryports = args.notryports | tryports = args.notryports | ||
port = args.port | port = args.port | ||
ipaddr = None | |||
if args.share: | |||
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) | |||
s.connect(("wikipedia.org",80)) | |||
ipaddr = s.getsockname()[0] | |||
s.close() | |||
while True: | while True: | ||
try: | try: | ||
server_address = ("", port) | if ipaddr: | ||
server_address = (ipaddr, port) | |||
servername = ipaddr | |||
else: | |||
server_address = ("localhost", port) | |||
servername = "localhost" | |||
httpd = server(server_address, handler) | httpd = server(server_address, handler) | ||
print "Serving | print "Serving at --> http://{0}:{1}".format(servername, port) | ||
httpd.serve_forever() | httpd.serve_forever() | ||
except socket.error, e: | except socket.error, e: | ||
Line 74: | Line 70: | ||
Error: port ({0}) is already in use | Error: port ({0}) is already in use | ||
==================================== | ==================================== | ||
You can pick another port number | You can pick another port number | ||
(for example 9999) with: | (for example 9999) with: | ||
serve --port 9999 | serve --port 9999 | ||
""".format(port) | """.format(port) | ||
Line 83: | Line 79: | ||
else: | else: | ||
raise(e) | raise(e) | ||
</source> | </source> |
Latest revision as of 17:56, 20 January 2014
In a time when the terms of "sharing" become increasingly contested and dictated by platforms vying for our personal data and attention, this simple one-liner, which shares the current directory (and potentially sub-directories) on one's local network takes on a political dimension. I share therefore I am!
Basic
python -m SimpleHTTPServer
With python 3, this would become...
python -m http.server 8888 &
Server with CGI support
To serve with CGI (just place CGI scripts in a folder named cgi-bin).
python -m CGIHTTPServer
Automatic port selection and nicer errors
#!/usr/bin/env python
import BaseHTTPServer, CGIHTTPServer
import cgitb; cgitb.enable() ## This line enables CGI error reporting
import sys, argparse, socket
from time import sleep
parser = argparse.ArgumentParser(description='Happy to serve you')
parser.add_argument('--port', type=int, default=8000, help='the port number to listen to')
parser.add_argument('-t', '--notryports', default=True, action="store_false", help='if a port is busy, automatically try other ones')
parser.add_argument('--share', default=False, action="store_true", help='Run as server accessible via your local network')
args = parser.parse_args()
server = BaseHTTPServer.HTTPServer
handler = CGIHTTPServer.CGIHTTPRequestHandler
handler.cgi_directories = ["/cgi-bin"]
tryports = args.notryports
port = args.port
ipaddr = None
if args.share:
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
s.connect(("wikipedia.org",80))
ipaddr = s.getsockname()[0]
s.close()
while True:
try:
if ipaddr:
server_address = (ipaddr, port)
servername = ipaddr
else:
server_address = ("localhost", port)
servername = "localhost"
httpd = server(server_address, handler)
print "Serving at --> http://{0}:{1}".format(servername, port)
httpd.serve_forever()
except socket.error, e:
if e.errno == 98:
if tryports:
if port < 2000:
port = 2000
else:
port += 1
sleep(.01)
else:
print """
====================================
Error: port ({0}) is already in use
====================================
You can pick another port number
(for example 9999) with:
serve --port 9999
""".format(port)
break
else:
raise(e)