Internet Modules and Tools
This section summarizes Python’s support for Internet scripting.
Commonly Used Library Modules
Following are 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.select
Interfaces to Unix and Windows
select
function. Waits for activity on one ofN
files or sockets. Commonly used to multiplex among multiple streams, or to implement timeouts. Only works 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 callsform=cgi.FieldStorage( )
,form
is a dictionary-like object with one entry per form field (e.g.,form["name"].value
is form fieldname
text).urllib
,urllib2
Fetches web pages and server script outputs from their Internet addresses (URLs):
urllib.urlopen(
url
)
returns file withread
methods; alsourllib.urlretrieve(
remote
,
local
)
. Supports HTTP, FTP, gopher, local file URLs. Also has tools for escaping URL text:urllib.quote_plus(
str
)
does URL escapes for text inserted into HTML output streams. ...
Get Python Pocket Reference, Second Edition 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.