Python
  Home arrow Python arrow Page 3 - 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

    Dell PowerEdge Servers

    Sockets in Python - Connecting to the Server
    (Page 3 of 5 )

    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:

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

    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 6 hosted by Hostway