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
x
10
10
1
GET /index.html HTTP/1.1
2
Host: localhost
3
Connection: keep-alive
4
Cache-Control: max-age=0
5
Upgrade-Insecure-Requests: 1
6
User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/52.0.2743.116 Safari/537.36
7
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8
8
Accept-Encoding: gzip, deflate, sdch
9
Accept-Language: en-US,en;q=0.8
10
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
1
18
18
1
import email
2
import pprint
3
from io import StringIO
4
5
request_string = 'GET / HTTP/1.1rnHost: localhostrnConnection: keep-alivernCache-Control: max-age=0rnUpgrade-Insecure-Requests: 1rnUser-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/52.0.2743.116 Safari/537.36rnAccept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8rnAccept-Encoding: gzip, deflate, sdchrnAccept-Language: en-US,en;q=0.8'
6
7
# pop the first line so we only process headers
8
_, headers = request_string.split('rn', 1)
9
10
# construct a message from the request string
11
message = email.message_from_file(StringIO(headers))
12
13
# construct a dictionary containing the headers
14
headers = dict(message.items())
15
16
# pretty-print the dictionary of headers
17
pprint.pprint(headers, width=160)
18
if you ran this at a python prompt, the result would look like:
JavaScript
1
9
1
{'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8',
2
'Accept-Encoding': 'gzip, deflate, sdch',
3
'Accept-Language': 'en-US,en;q=0.8',
4
'Cache-Control': 'max-age=0',
5
'Connection': 'keep-alive',
6
'Host': 'localhost',
7
'Upgrade-Insecure-Requests': '1',
8
'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/52.0.2743.116 Safari/537.36'}
9