2016-09-29 19:36:01 +00:00
|
|
|
#!/usr/bin/env python3
|
|
|
|
|
2018-03-26 18:57:50 +00:00
|
|
|
import os, sys, re
|
2016-09-29 19:36:01 +00:00
|
|
|
import http.server
|
|
|
|
import socketserver
|
|
|
|
|
2017-02-16 23:10:21 +00:00
|
|
|
class Handler(http.server.SimpleHTTPRequestHandler):
|
2016-09-29 19:36:01 +00:00
|
|
|
def do_GET(self):
|
2018-03-26 18:57:50 +00:00
|
|
|
|
|
|
|
response_body = ""
|
|
|
|
response_code = 200
|
2016-09-29 19:36:01 +00:00
|
|
|
|
|
|
|
if self.path == "/headers":
|
2018-03-26 18:57:50 +00:00
|
|
|
response_body += self.headers.as_string()
|
2016-09-29 19:36:01 +00:00
|
|
|
elif self.path == "/port":
|
2018-03-26 18:57:50 +00:00
|
|
|
response_body += "answer from port %s\n" % PORT
|
|
|
|
elif re.match("/status/(\d+)", self.path):
|
|
|
|
result = re.match("/status/(\d+)", self.path)
|
|
|
|
response_code = int(result.group(1))
|
|
|
|
response_body += "answer with response code %s\n" % response_code
|
2017-02-17 00:09:53 +00:00
|
|
|
elif self.path == "/":
|
2018-03-26 18:57:50 +00:00
|
|
|
response_body += "I'm %s\n" % os.environ['HOSTNAME']
|
2016-09-29 19:36:01 +00:00
|
|
|
else:
|
2018-03-26 18:57:50 +00:00
|
|
|
response_body += "No route for this path!\n"
|
|
|
|
response_code = 404
|
|
|
|
|
|
|
|
self.send_response(response_code)
|
|
|
|
self.send_header("Content-Type", "text/plain")
|
|
|
|
self.end_headers()
|
2016-09-29 19:36:01 +00:00
|
|
|
|
2018-03-26 18:57:50 +00:00
|
|
|
if (len(response_body)):
|
|
|
|
self.wfile.write(response_body.encode())
|
2017-02-16 23:10:21 +00:00
|
|
|
|
2016-09-29 19:36:01 +00:00
|
|
|
if __name__ == '__main__':
|
|
|
|
PORT = int(sys.argv[1])
|
|
|
|
socketserver.TCPServer.allow_reuse_address = True
|
2017-02-16 23:10:21 +00:00
|
|
|
httpd = socketserver.TCPServer(('0.0.0.0', PORT), Handler)
|
2016-09-29 19:36:01 +00:00
|
|
|
httpd.serve_forever()
|