Skip to content
Advertisement

How to parse raw HTTP request in Python 3?

I am looking for a native way to parse an http request in Python 3.

This question shows a way to do it in Python 2, but uses now deprecated modules, (and Python 2) and I am looking for a way to do it in Python 3.

I would mainly like to just figure out what resource is requested and parse the headers and from a simple request. (i.e):

JavaScript

Can someone show me a basic way to parse this request?

Advertisement

Answer

You could use the email.message.Message class from the email module in the standard library.

By modifying the answer from the question you linked, below is a Python3 example of parsing HTTP headers.

Suppose you wanted to create a dictionary containing all of your header fields:

JavaScript

if you ran this at a python prompt, the result would look like:

JavaScript
Advertisement