Parse http GET and POST parameters from BaseHTTPHandler in python?

Parse http GET and POST parameters from BaseHTTPHandler in python?

To parse HTTP GET and POST parameters from a BaseHTTPRequestHandler in Python, you can use the parse_qs function from the urllib.parse module. Here's an example of how to do it:

from http.server import BaseHTTPRequestHandler from urllib.parse import urlparse, parse_qs class MyHandler(BaseHTTPRequestHandler): def do_GET(self): self.send_response(200) self.send_header('Content-type', 'text/html') self.end_headers() # Parse the URL to get GET parameters parsed_url = urlparse(self.path) get_params = parse_qs(parsed_url.query) # Get specific GET parameters param_value = get_params.get('param_name', [''])[0] response = f"GET parameter value: {param_value}" self.wfile.write(response.encode()) def do_POST(self): self.send_response(200) self.send_header('Content-type', 'text/html') self.end_headers() # Get the length of the POST data content_length = int(self.headers['Content-Length']) # Read the POST data post_data = self.rfile.read(content_length).decode('utf-8') # Parse the POST data to get POST parameters post_params = parse_qs(post_data) # Get specific POST parameters param_value = post_params.get('param_name', [''])[0] response = f"POST parameter value: {param_value}" self.wfile.write(response.encode()) if __name__ == '__main__': from http.server import HTTPServer server_address = ('', 8080) httpd = HTTPServer(server_address, MyHandler) print('Starting server...') httpd.serve_forever() 

In this code:

  1. We create a custom BaseHTTPRequestHandler named MyHandler.
  2. In the do_GET method, we parse the URL to get the GET parameters using parse_qs. You can access specific GET parameters by their names.
  3. In the do_POST method, we first get the length of the POST data from the Content-Length header, then read and decode the POST data, and finally parse it to get POST parameters using parse_qs. Again, you can access specific POST parameters by their names.

This example demonstrates how to parse both GET and POST parameters from HTTP requests using the BaseHTTPRequestHandler. You can adapt it to your specific needs and extend it as necessary.

Examples

  1. "Python BaseHTTPHandler parse GET parameters example"

    • Description: Demonstrates how to extract HTTP GET parameters using Python's BaseHTTPHandler.
    from http.server import BaseHTTPRequestHandler, HTTPServer from urllib.parse import urlparse, parse_qs class MyHandler(BaseHTTPRequestHandler): def do_GET(self): parsed_url = urlparse(self.path) params = parse_qs(parsed_url.query) # Access params dictionary for further processing self.send_response(200) self.end_headers() self.wfile.write(b"GET parameters parsed successfully") def run(): server_address = ('', 8000) httpd = HTTPServer(server_address, MyHandler) httpd.serve_forever() if __name__ == '__main__': run() 
  2. "Python BaseHTTPHandler parse POST parameters example"

    • Description: Illustrates parsing HTTP POST parameters with Python's BaseHTTPHandler.
    from http.server import BaseHTTPRequestHandler, HTTPServer from urllib.parse import parse_qs class MyHandler(BaseHTTPRequestHandler): def do_POST(self): content_length = int(self.headers['Content-Length']) post_data = self.rfile.read(content_length).decode('utf-8') params = parse_qs(post_data) # Access params dictionary for further processing self.send_response(200) self.end_headers() self.wfile.write(b"POST parameters parsed successfully") def run(): server_address = ('', 8000) httpd = HTTPServer(server_address, MyHandler) httpd.serve_forever() if __name__ == '__main__': run() 

More Tags

chown github-flavored-markdown virtual-environment environment hamcrest angular6 nw.js react-hooks multi-level spring-mongo

More Python Questions

More Other animals Calculators

More Date and Time Calculators

More Gardening and crops Calculators

More Electronics Circuits Calculators