Home arrow Python arrow Page 4 - Sockets in Python

Sockets...Simplified - Python

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.

TABLE OF CONTENTS:
  1. Sockets in Python
  2. The Basics
  3. Connecting to the Server
  4. Sockets...Simplified
  5. Summary
By: Peyton McCullough
Rating: starstarstarstarstar / 113
December 28, 2004

print this article
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement

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' )
>>> 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.



 
 
>>> More Python Articles          >>> More By Peyton McCullough
 

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort
   

PYTHON ARTICLES

- Python Big Data Company Gets DARPA Funding
- Python 32 Now Available
- Final Alpha for Python 3.2 is Released
- Python 3.1: String Formatting
- Python 3.1: Strings and Quotes
- Python 3.1: Programming Basics and Strings
- Tuples and Other Python Object Types
- The Dictionary Python Object Type
- String and List Python Object Types
- Introducing Python Object Types
- Mobile Programming using PyS60: Advanced UI ...
- Nested Functions in Python
- Python Parameters, Functions and Arguments
- Python Statements and Functions
- Statements and Iterators in Python

Developer Shed Affiliates

 


Dev Shed Tutorial Topics: