-
Notifications
You must be signed in to change notification settings - Fork 0
/
simpleLoggingHTTPServer.py
executable file
·59 lines (47 loc) · 1.41 KB
/
simpleLoggingHTTPServer.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
#!/usr/bin/python
#
# Listen to specified port for HTTP queries.
# When a HTTP query is received it will log the raw request line and headers.
# If the HTTP query is a POST it will log the form fields
#
#
import SimpleHTTPServer
import SocketServer
import cgi
import getopt
import sys
port = 8080
class ServerHandler(SimpleHTTPServer.SimpleHTTPRequestHandler):
def do_GET(self):
SimpleHTTPServer.SimpleHTTPRequestHandler.do_GET(self)
print "HEADERS:"
print "--------"
print self.headers
def do_POST(self):
form = cgi.FieldStorage(
fp=self.rfile,
headers=self.headers,
environ={'REQUEST_METHOD':'POST',
'CONTENT_TYPE':self.headers['Content-Type'],
})
SimpleHTTPServer.SimpleHTTPRequestHandler.do_GET(self)
print "HEADERS:"
print "--------"
print self.headers
print "POST FIELDS:"
print "------------"
for item in form.list:
print item.name + " = " + item.value
try:
myopts, args = getopt.getopt(sys.argv[1:],"p:")
except getopt.GetoptError, e:
print (str(e))
print("Usage: %s -p port (Default 8080)" % sys.argv[0])
sys.exit(2)
for o, a in myopts:
if o == '-p':
port=int(a)
print "serving at port", port
Handler = ServerHandler
httpd = SocketServer.TCPServer(("", port), Handler)
httpd.serve_forever()