This section summarizes Python’s support for Internet scripting.
Following are summaries of some of the more commonly used modules in the Python Internet modules set. This is just a representative sample; see the Python Library Reference for a more complete list.
socket
Low-level network communications support (TCP/IP, UDP,
etc.). Interfaces for sending and receiving data over BSD-style
sockets: socket.socket()
makes an object with socket call methods (e.g., object.bind()
). Most protocol and
server modules use this module internally.
socketserver
(SocketServer
in Python
2.X)Framework for general threading and forking network servers.
xdrlib
Encodes binary data portably (also see socket
modules earlier in this
list).
select
Interfaces to Unix and Windows select
function. Waits for activity on
one of N
files or sockets.
Commonly used to multiplex among multiple streams or to
implement timeouts. Works only for sockets on Windows, not
files.
cgi
Server-side CGI script support: cgi.FieldStorage
parses the input
stream; cgi.escape
applies
HTML escape conventions to
output streams. To parse and access form information: after a
CGI script calls form=cgi.FieldStorage()
, form
is a dictionary-like object with
one entry per form field (e.g., form["name"].value
is form field
name
text).
urllib.request
(urllib
, urllib2
in Python 2.X)Fetches web pages and server script outputs from their
Internet addresses (URLs): urllib.request.urlopen(
url
)
returns file with read
methods; also urllib.request.urlretrieve(
remote
,
local
)
. Supports HTTP, FTP, gopher, and
local file URLs.
urllib.parse
(urlparse
in Python 2.X)Parses URL string into components. Also contains tools
for escaping URL text: urllib.parse.quote_plus(
str
)
does URL escapes for text inserted
into HTML output streams.
ftplib
FTP (file transfer protocol) modules. ftplib provides
interfaces for Internet file transfers in Python programs. After
ftp=ftplib.FTP('sitename')
, ftp
has methods for login, changing
directories, fetching/storing files and listings, etc. Supports
binary and text transfers; works on any machine with Python and
an Internet connection.
poplib
, imaplib
, smtplib
POP, IMAP (mail fetch), and SMTP (mail send) protocol modules.
email
packageParses and constructs email messages with headers and attachments. Also contains MIME support.
http.client
(httplib
in
Python 2), nntplib
, telnetlib
HTTP (web), NNTP (news), and Telnet protocol client modules.
http.server
(CGIHTTPServer
and SimpleHTTPServer
in Python 2.X)xml
package,
html
package (htmllib
in Python 2.X)Parse MXML documents and HTML web page contents. xml
package supports DOM, SAX, and
ElementTree parsing models.
xmlrpc
package (xmlrpclib
in Python 2.X)XML-RPC remote method call protocol.
uu
, binhex
, base64
, binascii
, quopri
Encodes and decodes binary (or other) data transmitted as text.
Table 1-24 lists some of these modules by protocol type.
Table 1-24. Selected Python Internet modules by protocol
Protocol | Common function | Port number | Python module |
---|---|---|---|
HTTP | Web pages | 80 |
|
NNTP | Usenet news | 119 |
|
FTP data default | File transfers | 20 |
|
FTP control | File transfers | 21 |
|
SMTP | Sending email | 25 |
|
POP3 | Fetching email | 110 |
|
IMAP4 | Fetching email | 143 |
|
Telnet | Command lines | 23 |
|