-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.py
More file actions
51 lines (38 loc) · 1.32 KB
/
server.py
File metadata and controls
51 lines (38 loc) · 1.32 KB
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
import socketserver
import http.server
import os
from urllib.parse import parse_qs
PORT = 5050
result = "hey"
class RequestHandler(http.server.SimpleHTTPRequestHandler):
def do_GET(self):
if self.path == "/":
self.path = "/index.html"
file_to_open = ""
try:
file_to_open = open(self.path[1:], 'r').read();
self.send_response(200)
except:
file_to_open = "File Not Found"
self.send_response(404)
self.send_header('Content-Type', 'text/html')
self.end_headers()
self.wfile.write(bytes(file_to_open, 'utf-8'))
def do_POST(self):
self.send_response(200)
self.send_header('Content-Type', 'text/html')
self.end_headers()
content_length = int(self.headers['Content-Length'])
post_data = self.rfile.read(content_length)
res = parse_qs(post_data.decode('utf-8'))
name = "-"
age = "-"
if 'name' in res: name = res['name'][0]
if 'age' in res: age = res['age'][0]
text = 'My name is {}. My age is {}'.format(name, age)
self.wfile.write(bytes(text, 'utf-8'))
httpd = socketserver.TCPServer(("localhost", PORT), RequestHandler)
try:
httpd.serve_forever()
except KeyboardInterrupt:
httpd.shutdown()