Blog: 使用 nginx 实现后端 HTTP 服务器的故障转移(failover): test-http-server.py

File test-http-server.py, 1.1 KB (added by thinkbase, 3 years ago)
Line 
1import sys
2import time
3import BaseHTTPServer
4
5HOST_NAME = 'localhost'
6PORT_NUMBER = 50080
7
8class TestHandler(BaseHTTPServer.BaseHTTPRequestHandler):
9    def do_HEAD(s):
10        s.send_response(200)
11        s.send_header("Content-type", "text/html")
12        s.end_headers()
13    def do_GET(s):
14        """Respond to a GET request."""
15        s.send_response(200)
16        s.send_header("Content-type", "text/html")
17        s.end_headers()
18        s.wfile.write("<html><head><title>%s %s</title></head>" %(HOST_NAME,PORT_NUMBER))
19        s.wfile.write("<body><p>%s %s</p>" %(HOST_NAME,PORT_NUMBER))
20       
21        s.wfile.write("<p>You accessed path: %s</p>" % s.path)
22        s.wfile.write("</body></html>")
23
24if __name__ == '__main__':
25    if sys.argv[1:]:
26        PORT_NUMBER = int(sys.argv[1])
27
28    server_class = BaseHTTPServer.HTTPServer
29    httpd = server_class((HOST_NAME, PORT_NUMBER), TestHandler)
30    print time.asctime(), "Server Starts - %s:%s" % (HOST_NAME, PORT_NUMBER)
31    try:
32        httpd.serve_forever()
33    except KeyboardInterrupt:
34        pass
35    httpd.server_close()
36    print time.asctime(), "Server Stops - %s:%s" % (HOST_NAME, PORT_NUMBER)