Skip to content

Commit b5bbd49

Browse files
committed
Add arg for port, show ip/dir
1 parent cc0ecdb commit b5bbd49

File tree

1 file changed

+29
-5
lines changed

1 file changed

+29
-5
lines changed

web.py

Lines changed: 29 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,35 @@
11
import SimpleHTTPServer
22
import SocketServer
3+
import sys
4+
import socket
5+
import os
36

4-
PORT = 8000
57

6-
Handler = SimpleHTTPServer.SimpleHTTPRequestHandler
8+
if len(sys.argv) > 1:
9+
try:
10+
PORT = int(sys.argv[1])
11+
except ValueError:
12+
PORT = -1
13+
else:
14+
PORT = 8000
715

8-
httpd = SocketServer.TCPServer(("", PORT), Handler)
16+
print "WinSimpleHTTP ----v1.1--------------------"
17+
# Port range
18+
# https://stackoverflow.com/questions/113224/what-is-the-largest-tcp-ip-network-port-number-allowable-for-ipv4#113228
19+
if PORT < 2 or PORT > 65535:
20+
print "Invalid Port : " + str(PORT)
21+
print "Port must be 2-65535"
22+
print "------------------------------------------"
23+
else:
24+
path = os.path.dirname(os.path.realpath(__file__))
925

10-
print "serving at port", PORT
11-
httpd.serve_forever()
26+
# How to get IP
27+
# https://stackoverflow.com/questions/166506/finding-local-ip-addresses-using-pythons-stdlib
28+
ip = [l for l in ([ip for ip in socket.gethostbyname_ex(socket.gethostname())[2] if not ip.startswith("127.")][:1], [[(s.connect(('8.8.8.8', 53)), s.getsockname()[0], s.close()) for s in [socket.socket(socket.AF_INET, socket.SOCK_DGRAM)]][0][1]]) if l][0][0]
29+
30+
31+
Handler = SimpleHTTPServer.SimpleHTTPRequestHandler
32+
httpd = SocketServer.TCPServer(("", PORT), Handler)
33+
print path + " ---> " + ip + ":" + str(PORT)
34+
print "------------------------------------------"
35+
httpd.serve_forever()

0 commit comments

Comments
 (0)