Building an HTTP client with urllib.request
The urllib.request
package is the recommended Python standard library package for HTTP tasks. The urllib
package has a simpler interface and it has the capacity to manage all tasks related to HTTP requests.
The urllib
module allows access to any resource published on the network (web page, files, directories, images, and so on) through various protocols (HTTP, FTP, and SFTP). To start consuming a web service, we have to import the following libraries:
#! /usr/bin/env python3 import urllib.request import urllib.parse
Using the urlopen
function, an object similar to a file is generated in which to read from the URL. This object has methods such as read
, readline
, readlines
, and close
, which work exactly the same as in file objects, although we are actually working with wrapper methods that abstract us from using low-level sockets.
Tip
The urllib.request
module allows access to a resource published on the internet through its...