Advertisement
Sophia_zhang

Backend server that returns the requested url

Jul 27th, 2023 (edited)
181
0
Never
2
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.04 KB | Software | 0 0
  1. from http.server import BaseHTTPRequestHandler, HTTPServer
  2.  
  3. class RequestHandler(BaseHTTPRequestHandler):
  4.     def do_GET(self):
  5.         # Get the current requested URL
  6.         current_url = self.path
  7.  
  8.         # Set the response status code
  9.         self.send_response(200)
  10.  
  11.         # Set the response headers
  12.         self.send_header('Content-type', 'text/html')
  13.         self.end_headers()
  14.  
  15.         # Create the HTML page with the current URL
  16.         html = f"""
  17.        <html>
  18.        <head>
  19.            <title>Current Requested URL</title>
  20.        </head>
  21.        <body>
  22.            <h1>Current Requested URL:</h1>
  23.            <p>{current_url}</p>
  24.        </body>
  25.        </html>
  26.        """
  27.  
  28.         # Send the HTML response to the client
  29.         self.wfile.write(html.encode('utf-8'))
  30.  
  31. def run_server(port=7000):
  32.     server_address = ('', port)
  33.     httpd = HTTPServer(server_address, RequestHandler)
  34.     print(f'Starting server on port {port}...')
  35.     httpd.serve_forever()
  36.  
  37. if __name__ == '__main__':
  38.     run_server()
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