Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does Python have a module for parsing HTTP requests and responses?

httplib (now http.client) and friends all have conn.getresponse() and an HTTPResponse class, but the server-side operations of conn.getrequest() and an HTTPRequest class seem to be lacking.

I understand that BaseHTTPServer and BaseHTTPRequestHandler can perform this functionality, but they don't expose these methods for use outside of the module.

Essentially what I want is BaseHTTPRequestHandler#parse_request to be a static method that returns an HTTPRequest object rather than populating member variables.

like image 566
Jeff Hammerbacher Avatar asked Jan 22 '10 06:01

Jeff Hammerbacher


People also ask

Which module in Python helps you to make the HTTP requests?

The requests module allows you to send HTTP requests using Python. The HTTP request returns a Response Object with all the response data (content, encoding, status, etc).

Which Python module is used for interacting with HTTP protocol?

urllib. urllib is a module built into the Python standard library and uses http. client which implements the client side of HTTP and HTTPS protocols.

What is Python module that you can use to send all kinds of HTTP requests?

Requests is a Python module that you can use to send all kinds of HTTP requests. It is an easy-to-use library with a lot of features ranging from passing parameters in URLs to sending custom headers and SSL Verification.

Does requests module come with Python?

requests is not part of the standard library. A default installation of Python will not include it.


2 Answers

Jeff, to enable parsing I create a small nine-line subclass of the base HTTP request handler:

from BaseHTTPServer import BaseHTTPRequestHandler from StringIO import StringIO  class HTTPRequest(BaseHTTPRequestHandler):     def __init__(self, request_text):         self.rfile = StringIO(request_text)         self.raw_requestline = self.rfile.readline()         self.error_code = self.error_message = None         self.parse_request()      def send_error(self, code, message):         self.error_code = code         self.error_message = message 

You can now take a string with the text of an HTTP request inside and parse it by instantiating this class:

# Simply instantiate this class with the request text  request = HTTPRequest(request_text)  print request.error_code       # None  (check this first) print request.command          # "GET" print request.path             # "/who/ken/trust.html" print request.request_version  # "HTTP/1.1" print len(request.headers)     # 3 print request.headers.keys()   # ['accept-charset', 'host', 'accept'] print request.headers['host']  # "cm.bell-labs.com"  # Parsing can result in an error code and message  request = HTTPRequest('GET\r\nHeader: Value\r\n\r\n')  print request.error_code     # 400 print request.error_message  # "Bad request syntax ('GET')" 
like image 54
Brandon Rhodes Avatar answered Sep 30 '22 12:09

Brandon Rhodes


For server-side processing you want to look at something like wsgiref.

The WSGI standard parses the request into a simple dictionary with all of the relevant headers and elements.

like image 25
S.Lott Avatar answered Sep 30 '22 13:09

S.Lott