Python
  Home arrow Python arrow Page 3 - Sockets in Python
Dev Shed Forums  
Administration  
AJAX  
Apache  
BrainDump  
DHTML  
Flash  
Java  
JavaScript  
Multimedia  
MySQL  
Oracle  
Perl  
PHP  
Practices  
Python  
Reviews  
Security  
Smartphone Development  
Style-Sheets  
Web Services  
XML  
Zend  
Zope  
Mobile Linux  
App Generation ROI  
IBM® developerWorks  
Forums Sitemap  
E-Commerce Hosting  
Linux Web Hosting  
Managed Hosting  
Small Business Hosting  
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? 
Google.com  
PYTHON

Sockets in Python
By: Peyton McCullough
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: starstarstarstarstar / 102
    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:
      error-file:tidyout.log Del.ici.ous error-file:tidyout.log Digg
      error-file:tidyout.log Blink error-file:tidyout.log Simpy
      error-file:tidyout.log Google error-file:tidyout.log Spurl
      error-file:tidyout.log Y! MyWeb error-file:tidyout.log 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


    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
     

       

    PYTHON ARTICLES

    - 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
    - Sequences and Sets in Python
    - Python Expressions and Operators
    - Dictionaries, Variables and Statements in Py...
    - Data Types in Python
    - The Python Language
    - SSH with Twisted





    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 2 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek