-
Notifications
You must be signed in to change notification settings - Fork 39
/
local-http-server.py
executable file
·75 lines (62 loc) · 2.18 KB
/
local-http-server.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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
#!/usr/bin/env python
# -*- coding: utf-8 -*-
###############################################################################
#
# local-http-server.py
# --------------------
# Serve a directory on the given address and port. Defaults to 127.0.0.1:8080.
#
# @authors: Isis Agora Lovecruft 0x2cdb8b35
# @version: 0.0.1
# @date: 25 December 2012
# @license: WTFPL
# @copyright: 2012 Isis Lovecruft
###############################################################################
import os
import sys
import BaseHTTPServer
from ipaddr import IPAddress
from optparse import OptionParser
from SimpleHTTPServer import SimpleHTTPRequestHandler
HandlerClass = SimpleHTTPRequestHandler
ServerClass = BaseHTTPServer.HTTPServer
Protocol = "HTTP/1.0"
HandlerClass.protocol_version = Protocol
parser = OptionParser()
parser.add_option('-p', '--port', dest='port', default=8080,
help="Local port to listen on")
parser.add_option('-a', '--address', dest='addr', default='127.0.0.1',
help="Local IP address to listen on")
parser.add_option('-d', '--dir', dest='dir', default='.',
help='[Not Implemented] Local directory to use as root directory of webserver')
if __name__ == "__main__":
if len(sys.argv[1:]) <= 0:
parser.print_help()
sys.exit(1)
else:
(options, args) = parser.parse_args()
if options.dir:
print "WARNING! The local directory option is not implemented."
try:
IPAddress(options.addr)
except ValueError, ve:
print ve.message
sys.exit(1)
else:
addr = options.addr
try:
port = int(options.port)
if port <= 1024 and os.getuid() != 0:
print "Server must be started as root to use port %d" % port
sys.exit(1)
except Exception, ex:
print ex.message
sys.exit(1)
httpd = ServerClass((addr, port), HandlerClass)
sa = httpd.socket.getsockname()
print "Serving HTTP on", sa[0], "port", sa[1], "..."
try:
httpd.serve_forever()
except KeyboardInterrupt, ki:
print "\nStopping server...\nExiting...\n"
sys.exit(0)