Python
  Home arrow Python arrow Page 3 - Sockets in Python: Into the World of P...
Dev Shed Forums 
Administration  
AJAX  
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 
Sun Developer Network 
Dedicated Servers 
E-Commerce Hosting 
Linux Web Hosting 
Managed Hosting 
Small Business Hosting 
Actuate Whitepapers 
VeriSign Whitepapers 
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: Into the World of Python Network Programming
By: A.P.Rajshekhar
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 5 stars5 stars5 stars5 stars5 stars / 32
    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:
      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: 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


       · This is my first article. Hope it has been useful. Please do comment.
       · very nice socket tutorial, especially for a first article! i like this article more...
       · Hi gimpy,Thank you for your compliments. Its readers like you who inspire me to...
       · Great tutorial, just what I was after and has aided my understanding a lot ;)Was...
       · HiThank you for your encouraging words and appreciation. If you can just tell me...
       · excellent article, we need more like this - there seems to be a severe lack of...
       · Thank you for your encouraging words. If you have any topics in your mind, do tell....
       · The reason I arrived at your very nice tutorial is that I would like to create a...
     

       

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





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