-
Notifications
You must be signed in to change notification settings - Fork 0
/
webserver.py
70 lines (65 loc) · 3 KB
/
webserver.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
from http.server import HTTPServer,BaseHTTPRequestHandler
import cgi
class webserverHandler(BaseHTTPRequestHandler):
def do_GET(self):
try:
if self.path.endswith("/hello"):
self.send_response(200)
self.send_header('Content-type', 'text/html')
self.end_headers()
output = ""
output += "<html><body>Hello!"
output += "<form method = 'POST' enctype='multipart/form-data' action='/hello'><h2>Waht would your message be?</h2><input name='message' type='text'><input type='submit' value='Submit'></form>"
output += "</body></html>"
# in python 3 you are required to encode a string to byte literals
output = bytes(output,encoding="ascii")
self.wfile.write(output)
return
if self.path.endswith("/hola"):
self.send_response(200)
self.send_header('Content-type','text/html')
self.end_headers()
output = ""
output += "<html><body>Hola!"
output += "<form method = 'POST' enctype='multipart/form-data' action='/hello'><h2>Waht would your message be?</h2><input name='message' type='text'><input type='submit' value='Submit'></form>"
output += "</body></html>"
# in python 3 you are required to encode a string to byte literals
output = bytes(output,encoding="ascii")
self.wfile.write(output)
return
except:
print("Inside except")
self.send_error(404,"File not found %s" %self.path)
def do_POST(self):
try:
print("Inside Post")
self.send_response(301)
self.send_header('Content-type', 'text/html')
self.end_headers()
print("Checkpoint")
ctype,pdict = cgi.parse_header(self.headers.getheader('content-type'))
if ctype == 'multipart/form-data' :
fields = cgi.parse_multipart(self.rfile,pdict)
messagecontent = fields.get('message')
output = ""
output += "<html><body>"
output += "<h2>How about this one :</h2>"
output += "<h1>%s</h1>" % messagecontent[0]
output += "<form method = 'POST' enctype='multipart/form-data' action='/hello'><h2>Waht would your message be?</h2><input name='message' type='text'><input type='submit' value='Submit'></form>"
output += "</body></html>"
#output = bytes(output,encoding="ascii")
self.wfile.write(output)
print(output)
except:
print("Inside Except of post")
pass
def main():
try:
port = 8080
server = HTTPServer(('',port),webserverHandler)
print("Web Server ruuning on port %s" % port)
server.serve_forever()
except KeyboardInterrupt:
pass
if __name__ == '__main__':
main()