>>> import w3lib.http
>>> w3lib.http.headers_raw_to_dict(b"Content-type: text/html\n\rAccept: gzip\n\n")
{'Content-type': 'text/html', 'Accept': 'gzip'}
>>> import w3lib.http
>>> w3lib.http.headers_raw_to_dict(b"Content-type: text/html\n\rAccept: gzip\n\n")
{'Content-type': ['text/html'], 'Accept': ['gzip']}
In [31]: copy_from_chrome = """Accept:text/html,application/xhtml+xml,applicati
...: on/xml;q=0.9,image/webp,*/*;q=0.8^M
...: Accept-Encoding:gzip, deflate, sdch^M
...: Accept-Language:zh-CN,zh;q=0.8^M
...: Cache-Control:max-age=0^M
...: Connection:keep-alive^M
...: Cookie:username-pes-8888="2|1:0|10:1494207240|17:username-pes-8888|48:
...: ODdmYWI4NmQtNDA0OC00Y2YzLTg3ZjYtOWE3Mzk0YmRiZTA2|3284b8f38c8d142ac8e71
...: 21c4dfd6f04d7548ccb6680f56e74a32c5f3f9dc3d4"^M
...: Host:pes^M
...: Upgrade-Insecure-Requests:1^M
...: User-Agent:Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHT
...: ML, like Gecko) Chrome/57.0.2987.133 Safari/537.36"""
In [32]: from w3lib.http import headers_raw_to_dict
In [33]: headers = headers_raw_to_dict(copy_from_chrome)
In [35]: headers
Out[35]:
{'Accept': ['text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/
*;q=0.8'],
'Accept-Encoding': ['gzip, deflate, sdch'],
'Accept-Language': ['zh-CN,zh;q=0.8'],
'Cache-Control': ['max-age=0'],
'Connection': ['keep-alive'],
'Cookie': ['username-pes-8888="2|1:0|10:1494207240|17:username-pes-8888|48:ODdm
YWI4NmQtNDA0OC00Y2YzLTg3ZjYtOWE3Mzk0YmRiZTA2|3284b8f38c8d142ac8e7121c4dfd6f04d75
48ccb6680f56e74a32c5f3f9dc3d4"'],
'Host': ['pes'],
'Upgrade-Insecure-Requests': ['1'],
'User-Agent': ['Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML,
like Gecko) Chrome/57.0.2987.133 Safari/537.36']}
In [36]: import requests
In [37]: z = requests.get(url,headers=headers)
In [39]: {i:headers[i][0] for i in headers}
Out[39]:
{'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*
;q=0.8',
'Accept-Encoding': 'gzip, deflate, sdch',
'Accept-Language': 'zh-CN,zh;q=0.8',
'Cache-Control': 'max-age=0',
'Connection': 'keep-alive',
'Cookie': 'username-pes-8888="2|1:0|10:1494207240|17:username-pes-8888|48:ODdmY
WI4NmQtNDA0OC00Y2YzLTg3ZjYtOWE3Mzk0YmRiZTA2|3284b8f38c8d142ac8e7121c4dfd6f04d754
8ccb6680f56e74a32c5f3f9dc3d4"',
'Host': 'pes',
'Upgrade-Insecure-Requests': '1',
'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, l
ike Gecko) Chrome/57.0.2987.133 Safari/537.36'}