Python smtplib Module

 < Day Day Up > 



Python smtplib Module

SMTP is the Simple Mail Transfer Protocol and is used to transfer e-mail to a mail server that delivers it to the final destination. Python provides a very simple SMTP interface, as is illustrated in Listing 11.2. Three methods are shown, which is all that's necessary in this simple example to send an e-mail.

Listing 11.2 Sample smtplib client example.

start example
import smtplib recip = "you@yourdomain.com" sender = "me@mydomain.com" server = "yourdomain.com" message = "From: me\nSubject: Hi\n\nHello\n\n" smtpSession = smtplib.SMTP( server ) smtpSession.sendmail( sender, recip, message ) smtpSession.quit() 
end example

The very simple source in Listing 11.2 illustrates sending a short e-mail. We make the smtplib module visible using import and then set up our recipient (recip) and source e-mail (sender) addresses. Next, we define the server (where the SMTP client will connect to send the e-mail). Then, we define the e-mail message to be sent in the message string.

The complete SMTP process is then shown in the final three lines. We create a new instance of an SMTP client using smtplib.SMTP, defining the server to which we'll connect to send our e-mail. Next, we start the SMTP dialog with the server using the sendmail method, to which we provide the sender, recipient, and e-mail body. Finally, to end the session, we use the finish method that permits the remote SMTP server to deliver the message.

Python httplib Module

HTTP is the classic Web transport protocol used to transfer Web pages (and other content) across the Internet. The Python HTTP module provides a very simple and powerful client-side interface that can be used for a variety of purposes. A sample usage of the HTTP client is shown in Listing 11.3.

Listing 11.3 Sample httplib client example.

start example
import httplib hcli = httplib.HTTPConnection( "www.mtjones.com" ) hcli.request( 'GET', '/index.html' ) resp = hcli.getresponse() if resp.status == 200:   data = resp.read()   print data else:   print "File not found." hcli.close()
end example

The result of the example shown in Listing 11.3 is the HTML source retrieved, the index.html file, from host www.mtjones.com. We first make the httplib module visible using the import statement and then create a new HTTP client instance using the httplib.HTTPConnection method, specifying the host to which we want to connect. Next, we request the file using the method request, specifying the particular HTTP method that we want to issue (in this case ‘GET'). We can identify the status of the request using the getresponse method that returns an HTTPResponse instance. Using this instance, we can check the success using the status attribute of the HTTPResponse object. If this is 200 (successful GET), then we issue a read on this HTTPResponse object to read the file. We then emit this using the print statement. If anything but a 200 status response was received, then we emit an error message. Finally, we close our HTTP client instance using the close method.

Python SocketServer Module

The SocketServer module helps simplify the development of TCP and UDP socket servers. It does this by providing a framework for which an application can define server information and then plug in handlers for serving content. Let's look at a simple example that reproduces our echo server discussed earlier in this chapter (see Listing 11.4).

Listing 11.4 Sample SocketServer example.

start example
import SocketServer import socket class EchoHandler( SocketServer.StreamRequestHandler ):   def handle( self ):     while 1:       line = self.rfile.readline()       self.wfile.write( line ) # Create a new SocketServer and begin serving serv = SocketServer.TCPServer( ('', 45000), EchoHandler ) serv.serve_forever()
end example

We first create our EchoHandler class that creates only one method, handle. This method is called once a client connects to the particular server and performs whatever actions are necessary to service the client. The handle method simply operates in an infinite loop reading a line from the socket and writing it back to the client. Note that the methods used here are file-descriptor methods rather than socket methods. The attribute self.rfile is used to read from the connection and self.wfile is used to write to it.

To create our new server, we use the SocketServer.TCPServer method, specifying the address to which we'll locally bind and the class that is to be used to handle requests. Finally, we call the method serve_forever to begin serving clients.



 < Day Day Up > 



BSD Sockets Programming from a Multi-Language Perspective
Network Programming for Microsoft Windows , Second Edition (Microsoft Programming Series)
ISBN: 1584502681
EAN: 2147483647
Year: 2003
Pages: 225
Authors: Jim Ohlund

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net