-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathweb-server-post.py
42 lines (27 loc) · 1.21 KB
/
web-server-post.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
# -*- coding: utf-8 -*-
import argparse
import urllib
from http.server import SimpleHTTPRequestHandler, HTTPServer
from socketserver import ThreadingMixIn
class ThreadedHTTPServer(ThreadingMixIn, HTTPServer):
pass
class RequestHandler(SimpleHTTPRequestHandler):
def do_POST(self):
data_string = self.rfile.read(int(self.headers['Content-Length']))
print(urllib.parse.unquote(data_string.decode('utf-8')))
SimpleHTTPRequestHandler.send_response(self, 200)
self.send_header("Content-Type", "text/html; charset=UTF-8")
SimpleHTTPRequestHandler.end_headers(self)
if __name__ == "__main__":
parser = argparse.ArgumentParser(description="Web Server")
parser.add_argument("-i", "--ip", help="IP/Host", action="store", default="127.0.0.1")
parser.add_argument("-p", "--port", help="Port", action="store", default=8000, type=int)
args = parser.parse_args()
print("Web server listening on {0}:{1}".format(args.ip, args.port))
server = ThreadedHTTPServer((args.ip, args.port), RequestHandler)
server.daemon_threads = True
try:
server.serve_forever()
except KeyboardInterrupt:
server.shutdown()
print("Shutting down...")