Search
 
SCRIPT & CODE EXAMPLE
 

PYTHON

how to parse http request in python

import email
import pprint
from io import StringIO

request_string = 'GET / HTTP/1.1
Host: localhost
Connection: keep-alive
Cache-Control: max-age=0
Upgrade-Insecure-Requests: 1
User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/52.0.2743.116 Safari/537.36
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8
Accept-Encoding: gzip, deflate, sdch
Accept-Language: en-US,en;q=0.8'

# pop the first line so we only process headers
_, headers = request_string.split('
', 1)

# construct a message from the request string
message = email.message_from_file(StringIO(headers))

# construct a dictionary containing the headers
headers = dict(message.items())

# pretty-print the dictionary of headers
pprint.pprint(headers, width=160)
Comment

PREVIOUS NEXT
Code Example
Python :: django group permissions method 
Python :: pyfiglet not coming up 
Python :: items of list 
Python :: python sounddevice stop recording 
Python :: python binary tree search 
Python :: how to average only positive number in array numpy 
Python :: ipython play audio 
Python :: python check if string contains one of characters list 
Python :: DecisionTreeClassifier 
Python :: search for list of strings in pandas column 
Python :: Access field values of form django 
Python :: Django - Knox auth setup 
Python :: time in python 
Python :: Python NumPy stack Function Example with 2d array 
Python :: loading a webpage with aiohttp 
Python :: python if string has spaces 
Python :: how to make a python file delete itself 
Python :: string format method python 
Python :: check dictionary values pandas dataframe colu 
Python :: python language server 
Python :: python with example 
Python :: python cartesian coordinates code 
Python :: reshape IML matrix 
Python :: python dataframe reihe anzeigen 
Python :: how to get csv file first row first column value in python 
Python :: how should i learn python 
Python :: how to take n space separated input in python” Code Answer’s 
Python :: pandas filter by dictionary 
Python :: return function in python 
Python :: declaring array size python 
ADD CONTENT
Topic
Content
Source link
Name
3+2 =