Sending an HTTP Request with 'urllib.request'


How to send an HTTP request using the "urllib.request" module?



To send an HTTP request to the Internet, you can use the urllib.request.urlopen() function as described below:

r = urllib.request.urlopen(url, data=None, [timeout, ]*, cafile=None, 
   capath=None, cadefault=False, context=None)

   url can be a URL string or Request object
   data is request body as a binary string. 

All urlopen() returns an http.client.HTTPResponse object. You can use its read() function to access the response body.

Here is a Python example on how to send a GET request and print the response body:

>>> import urllib
>>> r = urllib.request.urlopen('')
>>> b =
>>> print(b)
b'{\n  "args": {}, \n  "headers": {\n    
  "Accept-Encoding": "identity", \n    "Connection": "close", \n
  "Host": "", \n    "User-Agent": "Python-urllib/3.6"\n  }, \n  
  "url": ""\n}\n'


http.client.HTTPResponse Objects

What Is Python Module 'urllib'

'urllib' Module - Internet Communication

⇑⇑ Python Tutorials

2018-09-24, 159👍, 0💬