Advertisement
Sophia_zhang

Backend server that listens on 3 ports and returns requested ports number

Jul 29th, 2023
239
0
Never
2
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.19 KB | Software | 0 0
  1. #!/usr/bin/env python3
  2. '''Module: Starts three HTTP servers'''
  3. import os
  4. import time
  5. from http.server import BaseHTTPRequestHandler, HTTPServer
  6. from pprint import pprint
  7.  
  8. hostName = "localhost"
  9.  
  10. class MyServer(BaseHTTPRequestHandler):
  11.     def do_GET(self):
  12.         #print(self.server)
  13.         #print(self.headers)
  14.         self.send_response(200)
  15.         self.send_header("Content-type", "text/html")
  16.         self.end_headers()
  17.         self.wfile.write(bytes("""
  18.        <!DOCTYPE html>
  19.        <html>
  20.            <head>
  21.                <style> h1 {
  22.                            font-size:100px;
  23.                            text-align:center;
  24.                            margin-left:auto;
  25.                            margin-right:auto
  26.                           }
  27.                        p {
  28.                            font-size:20px;
  29.                            text-align:center;
  30.                          }
  31.                </style>
  32.                <title>%s</title>
  33.            </head>
  34.        <body>""" % self.headers['Host'] , "utf-8"))
  35.         self.wfile.write(bytes("<h1>{}</h1>".format(self.request.getsockname()[1]), "utf-8"))
  36.         self.wfile.write(bytes("<h1>{}</h1>".format(time.strftime('%X')), "utf-8"))
  37.         self.wfile.write(bytes("</body></html>", "utf-8"))
  38.  
  39. def start_server(port):
  40.     this_server = HTTPServer((hostName, port), MyServer)
  41.     print(time.strftime('%X'), "App server started - http://%s:%s" % (hostName, port))
  42.  
  43.     try:
  44.         this_server.serve_forever()
  45.     except KeyboardInterrupt:
  46.         pass
  47.  
  48.     this_server.server_close()
  49.     print(time.strftime('%X'), "App server stopped - http://%s:%s" % (hostName, port))
  50.  
  51. # list of the ports the servers will listen on
  52. PORTS = [7001, 7002, 7003]
  53.  
  54. # list to hold the PIDs from the forked servers
  55. SERVERS = []
  56.  
  57. print("Type CTRL+C to stop processing requests...")
  58.  
  59. # start a fork for each port
  60. for port in PORTS:
  61.     pid = os.fork()
  62.  
  63.     if pid:
  64.         SERVERS.append(pid)
  65.     else:
  66.         start_server(port)
  67.         exit(0)
  68.  
  69. # wait for the servers to finish, bailing out on CTRL+C
  70. for server in SERVERS:
  71.     try:
  72.         os.waitpid(server, 0)
  73.     except KeyboardInterrupt:
  74.         exit(0)
Advertisement
Comments
  • Sophia_zhang
    1 year
    Comment was deleted
  • Sophia_zhang
    1 year
    # text 0.15 KB | 0 0
    1. This is a script used for demoing the nginx load balance technique explained in this learning nginx blog: https://sophiazhang.com/post/start-with-nginx/
Add Comment
Please, Sign In to add comment
Advertisement