forked from skx/sysadmin-util
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpyhttpd
executable file
·61 lines (51 loc) · 1.33 KB
/
pyhttpd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
#!/usr/bin/env python2
#
# This is reworking of the Python one-liner HTTP server
#
# This reworking is required to allow the server to be bound to
# localhost-only, rather than listening on all interfaces, which
# is a potential security issue.
#
# By default you can launch a simple Python HTTP-server just by
# running:
#
# $ python -m SimpleHTTPServer 8080
#
# With this wrapper you can run:
#
# $ pyhttpd 127.0.0.1:3030
#
# or to revert to the all-interfaces behaviour just by specifing the
# port alone:
#
# $ pyhttpd 3044
#
# Steve
# --
#
import sys
from SimpleHTTPServer import SimpleHTTPRequestHandler
import BaseHTTPServer
def test(HandlerClass=SimpleHTTPRequestHandler,
ServerClass=BaseHTTPServer.HTTPServer):
protocol = "HTTP/1.0"
host = ''
port = 8000
if len(sys.argv) > 1:
arg = sys.argv[1]
if ':' in arg:
host, port = arg.split(':')
port = int(port)
else:
try:
port = int(sys.argv[1])
except:
host = sys.argv[1]
server_address = (host, port)
HandlerClass.protocol_version = protocol
httpd = ServerClass(server_address, HandlerClass)
sa = httpd.socket.getsockname()
print "Serving HTTP on", sa[0], "port", sa[1], "..."
httpd.serve_forever()
if __name__ == "__main__":
test()