High-level networking classes
Python includes several specialized modules for application-layer protocols (built on the standard socket module). The available modules are wide and varied and they provide module implementations of the Hypertext Transfer Protocol (HTTP), the Simple Mail Transfer Protocol (SMTP), Internet Message Access Protocol (IMAP) and Post Office Protocol (POP3), the Network News Transfer Protocol (NNTP), XML-RPC (remote procedure call), FTP, and many others.
This section demonstrates the modules shown in Table 4.
The HTTP client interface can be useful when developing Web robots or other Internet scraping agents. The Web protocol is request/response in nature over stream sockets. Python makes it easy to build Web robots through a simple Web interface.
Listing 25 demonstrates the
httplib module. You create a
new HTTP client instance with
HTTPConnection, providing the
Web site to which you want to connect. With this new object (
you can request files with the
request method. Within
request, you specify the HTTP
(which requests a file from the server, compared to
simply retrieves information about the file). The
method parses the HTTP response header to understand if an error was returned.
If the file was successfully retrieved, the
read method on the new
response object returns and prints the text.
Listing 25. Building a simple (non-rendering) HTTP client with
import httplib httpconn = httplib.HTTPConnection("www-130.ibm.com") httpconn.request("GET", "/developerworks/index.html") resp = httpconn.getresponse() if resp.reason == "OK": resp_data = resp.read() print resp_data httpconn.close()
SMTP allows you to send e-mail messages to a mail server which can be
useful in networking systems to relay status about the operation of a device. The
Python module for sending e-mail messages is simple and consists of creating
SMTP object, sending an e-mail message using the
sendmail method, then closing the connection with the
The example in Listing 26 demonstrates sending a simple e-mail message. The
msg string contains the body of the message (which should include
the subject line).
Listing 26. Sending a short e-mail message with
import smtplib fromAdrs = 'email@example.com' toAdrs = 'firstname.lastname@example.org' msg = 'From: email@example.com\r\nTo: firstname.lastname@example.org\r\nSubject:Hello\r\nHi!\r\n' mailClient = smtplib.SMTP('192.168.1.1') mailClient.sendmail( fromAdrs, toAdrs, msg ) mailClient.quit
POP3 is another useful application-layer protocol for which a module exists
within Python. The POP3 protocol allows you to connect to a mail server and
download new mail, which can be useful for remote commanding -- embedding
commands within the body of an e-mail message. After executing the embedded command,
you can use
smptlib to return a response e-mail message to
This demonstration in Listing 27 shows a simple application that connects to a mail server and emits the subject lines for all pending e-mail for the user.
poplib is relatively simple but offers several methods for
gathering and managing e-mail at the server. In this example, I create a new
POP3 object with the
POP3 method, specifying
the mail server. The
authenticate the application to the server; the
returns the number of messages waiting for the user and the total number of
bytes taken up by all messages.
Next, I loop through each available message and use the
method to grab the next e-mail message. This method returns a list of the form:
(response, ['line, ...], octets)
where response is the
POP3 response for the particular
message, the line
list represents the individual lines of the
e-mail message, and the final element, octets, is the number of bytes for
the e-mail message. The inner loop simply iterates over the second element ()
of the list which is the list of the e-mail message body. For each line, I test whether
'Subject:' is present; if so, I print this line.
After all e-mail messages have been checked, a call to the
method ends the POP3 session.
Instead of using the
retr method, you could also use the
method to extract the header for the e-mail message. This step would be faster and
minimize the amount of data transferred to this client.
Listing 27. Retrieving e-mail messages from a POP3 mail server and emitting the subject line
import poplib import re popClient = poplib.POP3('192.168.1.1') popClient.user('user') popClient.pass_('password') numMsgs, mboxSize = popClient.stat() print "Number of messages ", numMsgs print "Mailbox size", mboxSize print for id in range (numMsgs): for mail in popClient.retr(id+1): if re.search( 'Subject:', mail ): print mail print popClient.quit()