-
Notifications
You must be signed in to change notification settings - Fork 0
/
demohttpserver.py
39 lines (33 loc) · 1.05 KB
/
demohttpserver.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
#!/usr/bin/env python
import sys
import os
if sys.version_info >= (3, 0):
from http.server import BaseHTTPRequestHandler, HTTPServer
else:
from BaseHTTPServer import BaseHTTPRequestHandler, HTTPServer
class MyHandler(BaseHTTPRequestHandler):
def set_headers(self, status=200):
self.send_response(status)
self.send_header('Content-type', 'text/plain')
self.end_headers()
def do_GET(self):
if self.path == '/ping':
self.set_headers()
self.wfile.write(b'pong')
elif self.path == '/kill':
self.set_headers(status=500)
self.wfile.write(b'killed')
os._exit(1)
else:
self.set_headers(status=404)
self.wfile.write(b'404 Not Found')
def run(port=8000):
server_address = ('', port)
httpd = HTTPServer(server_address, MyHandler)
print('Starting HTTP server on port {}...'.format(port))
httpd.serve_forever()
if __name__ == "__main__":
if len(sys.argv) == 2:
run(port=int(sys.argv[1]))
else:
run()