xmlhttprequest - What is the suggested way to build and send a http request in Python -
i wrote small module 1 year age.
when tried add feature these days, found there big change in python's urllib
, confused how build request.
in old module fancyurlopener
used base class, yet found deprecated since version 3.3.
so read document again, , try build request instead of opener.
however, when tried add headers, 1 function request.add_header(key, val)
provided. have headers copied fiddler this:
get some_url http/1.1 host: sosu.qidian.com user-agent: mozilla/5.0 (windows nt 6.1; wow64; rv:33.0) gecko/20100101 firefox/33.0 accept: application/json, text/javascript, */*; q=0.01 accept-language: zh-cn,en-us;q=0.7,en;q=0.3 accept-encoding: gzip, deflate x-requested-with: xmlhttprequest referer: anothe_url cookie: lot of data connection: keep-alive
so have add them request 1 one?
also found openner urllib.request.build_opener()
can add lot of headers in 1 time. not set method 'get' or 'post'.
i newbie on python, suggestions ?
by far best way make http requests in python install , use module:
http://docs.python-requests.org/en/latest/
you can write code this:
headers = { 'x-requested-with': 'xmlhttprequest', 'referer': 'anothe_url', # etc } response = requests.get(some_url, headers=headers)
http://docs.python-requests.org/en/latest/user/quickstart/#custom-headers
Comments
Post a Comment