Python
  Home arrow Python arrow Page 3 - Sockets in Python: Into the World of Python Network Programming
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: Into the World of Python Network Programming
By: A.P.Rajshekhar
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: starstarstarstarstar / 50
    2005-11-28


    Table of Contents:
  • Sockets in Python: Into the World of Python Network Programming
  • Sockets Step-by-Step
  • A Simple Echo Server
  • Multi-Threaded Echo Server - Another Approach

  • 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: Into the World of Python Network Programming - A Simple Echo Server
    ( Page 3 of 4 )

    Now that the steps are clear, let's create a simple echo server. First the imports:

    from socket import *

    Then the constants that defines the host, port, buffer size and the address tuple to be used with bind().

    from socket import *
    HOST = 'localhost'
    PORT = 21567
    BUFSIZ = 1024
    ADDR = (HOST, PORT)

    Then we create the server side socket and bind it to the host and the port. Then comes the max queue size to 2:

    from socket import *
    HOST = 'localhost'
    PORT = 21567
    BUFSIZ = 1024
    ADDR = (HOST, PORT)
    serversock = socket(AF_INET, SOCK_STREAM)
    serversock.bind(ADDR)
    serversock.listen(2)

    Now, to make it listen for incoming requests continuously, place the accept() method in a while loop. This is not the most preferred mode. The preferred way will be discussed in the next section:

    from socket import *
    HOST = 'localhost'
    PORT = 21567
    BUFSIZ = 1024
    ADDR = (HOST, PORT)
    serversock = socket(AF_INET, SOCK_STREAM)
    serversock.bind(ADDR)
    serversock.listen(2)

    while 1:
           print 'waiting for connection…'
           clientsock, addr = serversock.accept()
           print '…connected from:', addr
           :
           :

    Next, receive the data from the client and echo it back. This has to continue until the client doesn’t send the null data or ctrl+c. To achieve this, use a while loop again and then close the connection when done.

    from socket import *
    HOST = 'localhost'
    PORT = 21567
    BUFSIZ = 1024
    ADDR = (HOST, PORT)
    serversock = socket(AF_INET, SOCK_STREAM)
    serversock.bind(ADDR)
    serversock.listen(2)

    while 1:
           print 'waiting for connection…'
           clientsock, addr = serversock.accept()
           print '…connected from:', addr

           while 1:
                 data = clientsock.recv(BUFSIZ)
                 if not data: break
                       clientsock.send(‘echoed’, data)
      
           clientsock.close()

    serversock.close()

    That’s all for the server. Now for the client. The only exception is that there is no bind(), accept() and listen().

    from socket import *

    HOST = 'localhost'
    PORT = 21567
    BUFSIZ = 1024
    ADDR = (HOST, PORT)

    tcpCliSock = socket(AF_INET, SOCK_STREAM)
    tcpCliSock.connect(ADDR)

           while 1:
                 data = raw_input('> ')
                 if not data: break 
                 tcpCliSock.send(data)
                data = tcpCliSock.recv(1024)
                 if not data: break 
           print data

    tcpCliSock.close()




     
     
    >>> More Python Articles          >>> More By A.P.Rajshekhar
     

       

    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 1 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek