Tools, FAQ, Tutorials:
Using urllib.request.Request Object
How to use the urllib.request.Request object to build more complex HTTP request?
✍: FYIcenter.com
The urllib.request.urlopen() function can take a
urllib.request.Request object to let you building more complex HTTP requests.
You can use the Request() constructor to build a Request object:
q = urllib.request.Request(url, data=None, headers={}, origin_req_host=None, unverifiable=False, method=None) where: url should be a string containing a valid URL. data is request body as a binary string, file-like object, or iterable. headers should be a dictionary of request headers.
Here is a Python example on how to build a complex HTTP POST request using a urllib.request.Request object:
>>> import urllib >>> url = 'http://httpbin.org/post' >>> form = {'user':'fyicenter', 'msg':'hello world!'} >>> data = urllib.parse.urlencode(form) >>> data = bytes(data,'utf8') >>> headers = {'Referer':'http://dev.fyicenter.com'} >>> req = urllib.request.Request(url,data,headers,method='POST') >>> r = urllib.request.urlopen(req) >>> b = r.read() >>> print(b) b'{\n "args": {}, \n "data": "", \n "files": {}, \n "form": {\n "msg": "hello world!", \n "user": "fyicenter"\n }, \n "headers": {\n "Accept-Encoding": "identity", \n "Connection": "close", \n "Content-Length": "33", \n "Content-Type": "application/x-www-form-urlencoded", \n "Host": "httpbin.org", \n "Referer": "http://dev.fyicenter.com", \n "User-Agent": "Python-urllib/3.6"\n }, \n "url": "http://httpbin.org/post"\n}\n'
⇒ urllib.request.Request Properties and Functions
⇐ Using urllib.parse.urlencode()
2018-09-13, 3907👍, 0💬
Popular Posts:
What is EPUB 3.0 Metadata "dc:publisher" and "dc:rights" elements? EPUB 3.0 Metadata "dc:publisher" ...
What is EPUB 3.0 Metadata "dc:publisher" and "dc:rights" elements? EPUB 3.0 Metadata "dc:publisher" ...
How to add images to my EPUB books Images can be added into book content using the XHTML "img" eleme...
How to use the XML to JSON Conversion Tool at freeformatter.com? If you want to try the XML to JSON ...
How to use the Atom Online Validator at w3.org? w3.org feed validation service is provided at http:/...