Book Image

Learning Python Networking - Second Edition

By : José Manuel Ortega, Dr. M. O. Faruque Sarker, Sam Washington
Book Image

Learning Python Networking - Second Edition

By: José Manuel Ortega, Dr. M. O. Faruque Sarker, Sam Washington

Overview of this book

Network programming has always been a demanding task. With full-featured and well-documented libraries all the way up the stack, Python makes network programming the enjoyable experience it should be. Starting with a walk through of today's major networking protocols, through this book, you'll learn how to employ Python for network programming, how to request and retrieve web resources, and how to extract data in major formats over the web. You will utilize Python for emailing using different protocols, and you'll interact with remote systems and IP and DNS networking. You will cover the connection of networking devices and configuration using Python 3.7, along with cloud-based network management tasks using Python. As the book progresses, socket programming will be covered, followed by how to design servers, and the pros and cons of multithreaded and event-driven architectures. You'll develop practical clientside applications, including web API clients, email clients, SSH, and FTP. These applications will also be implemented through existing web application frameworks.
Table of Contents (19 chapters)
Free Chapter
1
Section 1: Introduction to Network and HTTP Programming
4
Section 2: Interacting with APIs, Web Scraping, and Server Scripting
9
Section 3: IP Address Manipulation and Network Automation
13
Section 4: Sockets and Server Programming

Consuming web services in Python with urllib

In this section, we will learn how to use urllib and how we can build HTTP clients with this module.

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, SFTP). To start consuming a web service, we have to import the following libraries:

#! /usr/bin/env python3
import urllib.request
import urllib.parse

There are four functions in urllib:

  • request: Opens and reads the request's URL
  • error: Contains the errors generated by the request
  • parse: A tool to convert the URL
  • robotparse: Converts the robots.txt files

The urllib.request module allows access to a resource published on the internet through its address. If we go to the documentation of the Python 3 module (https://docs.python.org/3/library/urllib.request.html#module-urllib...