Sei sulla pagina 1di 5

Dev Shed 04/20/2007 11:45:36 PM

Python
Sockets in Python
Contributed by Peyton McCullough
2004−12−28
Essential protection from viruses, hackers and privacy threats − Norton Internet Security 2007 Click Here
Purchase Norton Internet Security 2007 via the US store Today! Click Here

Sockets are the lead pipes of computer networks: they let you connect with other devices so that information can flow
freely. As you might expect, they're widely used on the Internet. Peyton McCullough explains how to code sockets in
Python.

Introduction

One of the must−know features of any language is sockets, and, no, I’m not talking about the ones positioned on your
ceiling. While many novice scripters will run away at the sound of the word, sockets in Python are not hard to learn and
work with at all. In this article, I will explain the basics and even apply sockets in a real−world situation.

Please note that this article assumes that you have a basic command of the Python language. If not, you can either read the
official tutorial or buy a book – or both. If you’re looking for a good book, you should head over to O’Reilly &
Associates.

With that stated, let’s get started.

The Anatomy of a Socket

Let’s start off simple: what is a socket? The easiest way to learn is to compare computer sockets to lead pipes. Let’s say
you have some sort of machine that has two pipes. One pipe pumps in water and the other pipe pumps out water. This will
be your server. Now, imagine you have a simple box that has one pipe going through it. Let’s say that you hook it up to
your pump machine. It will receive the water from the machine and then give water back. This will be your client. If you
wanted to, you could hook up more machines and pipes to this to form a network of clients and servers.

Sockets are useful. The Web browser that you are viewing this page with is connecting to the server that this page is in by
way of a socket. If you were to connect to one of your favorite online games or chat networks, you would be using sockets
as well. As you can see, sockets are both both powerful and widely−used.

Using sockets in Python is quite easy. Open up your Python command line, and let’s get to some code.

The first thing we must do is import the socket library:

>>> import socket

The socket library contains all the tools we need to work with sockets. The next thing we need to do is create a socket.
This is simple. Execute the following code:

>>> mySocket = socket.socket ( socket.AF_INET, socket.SOCK_STREAM )

This creates a stream socket. You can also work with datagrams by replacing SOCK_STREAM with SOCK_DGRAM. A
socket stream is where there is a constant connection between the client and server that stays alive until it is closed, and

1/5
Dev Shed 04/20/2007 11:45:36 PM
both the client and the server know if the connection is still alive. With datagrams, however, that is not the case. The
connection is not kept alive, and your data might not even be received. Although datagrams can sound like a bad idea at
first, they have their purposes. It might be easier and faster to use datagrams in certain situations.

Writing a Simple Server

Let’s write a simple server. If you’ll remember, a server is anything that receives connections from other computers,
clients. Create a new Python file named server.py and insert the following code into it:

import socket
mySocket = socket.socket ( socket.AF_INET, socket.SOCK_STREAM )
mySocket.bind ( ( '', 2727 ) )
mySocket.listen ( 1 )
while True:
channel, details = mySocket.accept()
print 'We have opened a connection with', details
print channel.recv ( 100 )
channel.send ( 'Green−eyed monster.' )
channel.close()

That’s quite a mouthful, so let’s split it up into something we can understand. The first two lines should already look
familiar. We create a new socket to use. In the third line, we open port 2727 for connections.

To understand what a port is, let’s go back to our analogy. Picture the pump machine with thousands of pipes leading in
and out of it. Each pipe would be a port, and clients would have the option of connecting to different ports. However, each
port would be different – some might pump out green water, and others might pump out orange water.

The next line tells our socket to wait, or listen, for clients. Following that, there is an infinite loop. In this loop, we accept
client connections, print out the client's address, print out the client's message and finally send a message back, closing the
connection when we're done.

Let's connect to our server program. Open up the Python command line again and create a socket:

>>> import socket


>>> mySocket = socket.socket ( socket.AF_INET, socket.SOCK_STREAM )

Next, open your server program. We will now connect to the server:

>>> mySocket.connect ( ( 'localhost', 2727 ) )

This should look very familiar to the bind method, with the exception of the first argument, which tells our socket where
to connect to. We now need to send our server a message:

>>> mySocket.send ( 'William Shakespeare' )

You should now see the message in the server window. Let's accept the server's reply:

>>> mySocket.recv ( 100 )

Finally, let's clean up by closing the socket:

2/5
Dev Shed 04/20/2007 11:45:36 PM

>>> mySocket.close()

That wasn't so hard, was it? Of course not. As I said, sockets are extremely easy to learn and use in your scripts.

Datagrams

I explained datagrams a little bit in a previous section, but I will now show you how to work with datagrams. Let's take
our server and rewrite it using datagrams:

import socket
mySocket = socket.socket ( socket.AF_INET, socket.SOCK_DGRAM )
mySocket.bind ( ( '', 2727 ) )
while True:
data, client = mySocket.recvfrom ( 100 )
print 'We have received a datagram from', client
print data
mySocket.sendto ( 'Green−eyed datagram.', client )

While our datagram server is very similar to our stream server, it also has obvious differences. Notice how we never use
the listen method, and note how we use recvfrom and sendto rather than accept, recv and send.

Let's connect to our server through the command line:

>>> import socket


>>> mySocket = socket.socket ( socket.AF_INET, socket.SOCK_STREAM )
>>> mySocket.sendto ( 'Wherefore art thou?', ( 'localhost', 2727 ) )
>>> data, server = mySocket.recvfrom ( 100 )
>>> print data

As with the server, it is both similar to and different from the other client's code.

While the socket library is good for many things, there are several libraries built on the socket library that make
development easier and faster. Say you wanted to connect to the official Python site to retrieve a piece of information.
You could accomplish this task with the socket library, but there's no use working at such a low level when other libraries
could do this in a shorter amount of lines. There are libraries for HTTP, FTP, Gopher, telnet and more.

A Real−World Problem and a Pythonic Solution

Let's say that you need to convert United States Dollars into Euros. This may seem complex at first, but it really isn't. It
can be accomplished with sockets. We'll use the urllib library to connect to 'http://xe.com' and convert ten dollars into
Euros.

Open up the Python command line and import the urllib module:

>>> import urllib

Connecting to the currency converter website I mentioned is simple with the urllib library −− a lot simpler than it would
have been if I had used the socket library alone. It can be accomplished with the following line of code:

>>> currency = urllib.urlopen (


'http://xe.com/ucc/convert.cgi?Amount=10&From=USD&To=EUR' )

3/5
Dev Shed 04/20/2007 11:45:36 PM
>>> data = currency.read()

If you attempt to print out the data variable, you will get a good bit of junk that is not necessary in our script. However,
here are the key lines:

<TD ALIGN=RIGHT><FONT FACE="Arial,Helvetica">


<FONT SIZE=+1><B>10.00 USD</B></FONT><FONT SIZE=−1><BR>United States
Dollars
</FONT></TD>
<TD> </TD>
<TD ALIGN=CENTER><FONT FACE="Arial,Helvetica"
SIZE=+3><B>=</B></FONT></TD>
<TD> </TD>
<TD ALIGN=LEFT><FONT FACE="Arial,Helvetica">
<FONT SIZE=+1><B>7.46213 EUR</B></FONT><FONT SIZE=−1><BR>Euro
</FONT></TD>

( Note that the values above are outdated. )

We can use regular expressions to sort through all the junk and take out the figures we need from the above code:

>>> import re
>>> euros = re.search ( '(\d*)\.(\d*) EUR', data )
>>> print '10 United States Dollars are equal to...' >>> print
euros.group ( 1 ) + '.' + euros.group ( 2 ), 'Euros.'

We also need to clean up and close the connection like we did earlier:

>>> currency.close()

That's it, we've solved our problem in only a few lines! If we were to do it with the socket library, however, it would have
required a number of more lines to accomplish the same result.

You have learned that sockets are a valuable tool in the Python scripting language. They enable you to communicate with
other computers, clients and servers, and to exchange data. Sockets are used to accomplish many common
computer−related tasks, such as browsing Web pages. Though many novices assume that sockets are difficult, the
opposite is true. Python also includes a number of libraries built on top of the socket library that allow you to easily write
networking applications.

You now have the knowledge and tools to construct scripts that use sockets. Where you choose to go from here is entirely
up to you. However, you may wish to browse through the standard library to find modules to aid development using
sockets. Some modules that may help you include the select, asyncore, asynchat, ftplib ( FTP ), poplib ( mail ), imaplib (
mail ), smtplib ( mail ), telnetlib ( telnet ), nnttplib ( news ), SocketServer ( server ), BaseHTTPServer ( HTTP server ),
SimpleHTTPServer ( HTTP server ) and CGIHTTPServer ( HTTP server ) modules.

DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content
provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts,
and/or product reviews. As such it is incumbent upon the reader to employ real−world tactics for security and
implementation of best practices. We are not liable for any negative consequences that may result from implementing any

4/5
Dev Shed 04/20/2007 11:45:36 PM

information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify
your hardware.

5/5

Potrebbero piacerti anche