-
Notifications
You must be signed in to change notification settings - Fork 0
/
ForkStaticServer.py
executable file
·56 lines (43 loc) · 1.32 KB
/
ForkStaticServer.py
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
#!/usr/bin/env python
import socket
import SocketServer
import sys
import os
from SimpleHTTPServer import SimpleHTTPRequestHandler
class ForkingHTTPServer(SocketServer.ForkingTCPServer):
allow_reuse_address = 1
def server_bind(self):
"""Override server_bind to store the server name."""
SocketServer.TCPServer.server_bind(self)
host, port = self.socket.getsockname()[:2]
self.server_name = socket.getfqdn(host)
self.server_port = port
def run(HandlerClass = SimpleHTTPRequestHandler,
ServerClass = ForkingHTTPServer, protocol="HTTP/1.0"):
host = ''
port = 8001
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]
if len(sys.argv) > 2:
dir = sys.argv[2]
try:
os.chdir(dir)
except:
print "The specified [ %s ] directory is invalid." %dir
exit(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__':
run()