Python
  Home arrow Python arrow Page 4 - 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? 
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 - 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.



     
     
    >>> 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 2 Hosted by Hostway
    Stay green...Green IT