Python
  Home arrow Python arrow Page 4 - 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 - Multi-Threaded Echo Server - Another Approach


    (Page 4 of 4 )

    The exampl in the previous section uses the while loop to service different clients. For elucidations it is okay. But in the real world, it won’t work well. The reason is that more than one client cannot be served simultaneously with just while constructs. To overcome the limitations there are several strategies. One of them involves making the server multi-threaded. There are two parts to the creation of a multi threaded server:

    1. Create threads for each accepted  connections
    This is the core of the multi-threaded server. For each accepted connection request, a different thread is created and the serving of that particular client is carried out by an independent thread. Thus quick response times can be achieved.
     
    2. Create a handler
    It is the handler where the whole processing goes on. In our case, this means  transferring a file.

    To make the echo server some changes need to be made. It starts with the accept part as shown below:

    from socket import *
    from threading 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
           thread.start_new_thread(handler, (clientsock,
    addr))
     
    serversock.close()

    After accepting the request, a new thread is created for the client. This is done for each connection request. The logic of handling the client is defined within the handler, which looks like this:

    from socket import *
    from threading import *
    def handler(clientsock,addr):
           while 1:
                 data = clientsock.recv(BUFSIZ)
                 if not data: break
                       clientsock.send(‘echoed:..’, data)
      
           clientsock.close()
    if __name__==’__main__’:
           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
                 thread.start_new_thread(handler, (clientsock, addr))
    #some other cleanup code if necessary

    The handler has to be defined before calling it. The handler contains the same code that was contained in the inner while loop previously. This example is not optimized. But it serves the purpose of providing a different approach for serving multiple clients. 

    Parting Thoughts

    Here are a few points to keep in mind:

    • The low level sockets can be mixed and matched with other modules such as threads and forks to create a server capable of serving multiple clients simultaneously.
    • While using the threading approach, the locking and synchronization issues must be kept in mind.
    • Security measures must be taken when creating FTP servers.

    This brings us to the end of this discussion. In the introductory section I mentioned flexibility as one of the core aspects of Python. Ability to work with low-level sockets is one of them. But at the other end of the spectrum are the pre-built yet extensible web servers. These will be discussed in the near future.


    DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware.

       · 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