Python Network Programming 简明教程

Python - Internet Protocol

互联网协议旨在在所有联网计算机上实现统一的地址系统,让数据包能够从互联网的一端传送到另一端。像 Web 浏览器这样的程序应能够连接到任何地方的主机,而无需了解每个数据包在传输过程中经过的网络设备迷宫。互联网协议有各种类别。这些协议旨在满足互联网中不同计算机之间不同类型数据通信的需求。

The Internet Protocol is designed to implement a uniform system of addresses on all of the Internet-connected computers everywhere and to make it possible for packets to travel from one end of the Internet to the other. A program like the web browser should be able to connect to a host anywhere without ever knowing which maze of network devices each packet is traversing on its journey. There are various categories of internet protocols. Thes protocols are created to serve the needs of different types of data communication between different computers in the internet.

Python 有几个模块来处理每个这些通信场景。这些模块中的方法和功能可以完成简单的验证 URL 的任务或处理 cookie 和会话的复杂任务。在本章,我们将介绍用于互联网协议的最著名的 Python 模块。

Python has several modules to handle each of these communication scenarios. The methods and functions in these modules can do the simplest job of just validating a URL or also the complex job of handling the cookies and sessions. In this chapter we will look at the most prominent python modules used for internet protocols.

Protocol

Python Module Name

Description

HTTP

urllib.request

Opening the HTTP URL

HTTP

urllib.response

Create a reponse object for a url request

HTTP

urllib.parse

To break Uniform Resource Locator (URL) strings up in components like (addressing scheme, network location, path etc.),

HTTP

urllib.robotparser

It finds out whether or not a particular user agent can fetch a URL on the Web site that published the robots.txt file.

FTP

ftplib

implements the client side of the FTP protocol. You can use this to write Python programs that perform a variety of automated FTP jobs, such as mirroring other FTP servers.

POP

poplib

This module defines a class, POP3, which encapsulates a connection to a POP3 server to read messages from a email server

IMAP

imaplib

This module defines three classes, IMAP4, IMAP4_SSL and IMAP4_stream, which encapsulate a connection to an IMAP4 server to read emails.

SMTP

smtplib

The smtplib module defines an SMTP client session object that can be used to send mail to any Internet machine with an SMTP listner deamon.

Telnet

telnet

This module provides a Telnet class that implements the Telnet protocol to access a server thorugh teleent.

每个类都会在后续章节中详细阐述。

Each of them is discussed in detail in the subsequent chapters.