Introduction to urllib2

urllib2 can read data from a URL using various protocols, such as HTTP, HTTPS, FTP, or Gopher. This module provides urlopen function used to create an object similar to a file with which can to read from the URL. This object has methods such as read(), readline(), readlines(), and close(), which work exactly the same as in the file objects, although in reality we are working with a wrapper that abstracts us from using a socket at low level.

The read method, as you will remember, is used to read the complete "file" or the number of bytes specified as a parameter, readline to read a line, and readlines to read all the lines and return a list with them.

We also have a couple of geturl methods, to get the URL of the one ...

Get Mastering Python for Networking and Security now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.