Python
  Home arrow Python arrow Page 4 - Sockets in Python
Dev Shed Forums 
Administration  
Apache  
BrainDump  
DHTML  
Flash  
Java  
JavaScript  
Multimedia  
MySQL  
Oracle  
Perl  
PHP  
Practices  
Python  
Reviews  
Security  
Style-Sheets  
Web Services  
XML  
Zend  
Zope  
Forums Sitemap 
IBM® developerWorks 
Dedicated Servers 
E-Commerce Hosting 
Linux Web Hosting 
Managed Hosting 
Small Business Hosting 
Download TestComplete 
VPS Hosting 
Weekly Newsletter

 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
PYTHON

Sockets in Python
By: Peyton McCullough
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 88
    2004-12-28

    Table of Contents:
  • Sockets in Python
  • The Basics
  • Connecting to the Server
  • Sockets...Simplified
  • Summary

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
     
    ADVERTISEMENT

    Stay one step ahead of the competition. Evaluate and give feedback on some of the hottest web development tools on the market today. Make your opinion heard! Click Here

    Sockets in Python - Sockets...Simplified
    (Page 4 of 5 )

    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


       · I´m new to python and very thankful for this fine and concise article on Sockets in...
       · Thanks. I must agree with you on the time-saving part, by the way. With Python,...
       · Hi there.. listen() is about the specifying the size of the queue of connections to...
       · Sorry, that's a bad habit of mine for setting the maximum amount of...
       · what about in a script...you did the same thing everyone does...you show how to do...
       · In that case, you would simply put the same code into a script.
       · I'm getting the following when using your tutorial:>> mySocket.sendto('Wherefore...
       · I think there's an error in the client code for datagrams in this article. The...
       · nice article :)) and ty very much
     

       

    PYTHON ARTICLES

    - SSH with Twisted
    - Mobile Programming in Python using PyS60: UI...
    - Python: Count on It
    - Python Strings: Spinning Yarns
    - Python: More Fun with Strings
    - Python: Stringing You Along
    - Python Operators
    - Bluetooth Programming in Python: Network Pro...
    - Python Sets
    - Python Conditionals, Lists, Dictionaries, an...
    - Python: Input and Variables
    - Introduction to Python Programming
    - Mobile Programming in Python using PyS60: Ge...
    - Bluetooth Programming using Python
    - Finishing the PyMailGUI Client: User Help To...

     
    Accelerating Trading Partner Performance
     
    Competing on Analytics
     
    Cost Effective Scaling with Virtualization and Coyote Point Systems
     
    Five Checkpoints to Implementing IP Telephony
     
    Hosted Email Security: Staying Ahead of New Threats
     




    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 5 hosted by Hostway