Python
  Home arrow Python arrow Sockets in Python: Into the World of P...
Dev Shed Forums 
Administration  
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 
Dedicated Servers 
E-Commerce Hosting 
Linux Web Hosting 
Managed Hosting 
Small Business Hosting 
Download TestComplete 
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 / 31
    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


    Sockets in Python: Into the World of Python Network Programming
    (Page 1 of 4 )

    Python offers network programmers a variety of options and an excellent degree of flexibility for tackling various situations. This article shows you how to take advantage of that flexibility by using raw sockets to create network oriented applications.

    “Code less, achieve more” is the prime philosophy behind the development of all the Very High Level Languages (or VHLL in short). But a fewer number of lines should not mean reduced flexibility in terms of choosing an approach for solving a problem. Though many of the VHLL, or scripting languages as they are popularly known, do not keep flexibility in mind the flexibility, there are a few that have the logic of flexibility and choice as their core.

    Python is one of them. This fact is evident if one tries to do network programming in Python. There are plenty of choices for the programmer. These range from low-level sockets or raw-sockets to a completely extensible and functional web server.

    In this tutorial I will be discussing how to use raw sockets to create network oriented applications in Python. In the early part, I will cover the basics of the socket module,  and a simple echo server will be coded. Later on, the echo server will be enhanced by making it capable of serving multiple clients using the concepts introduced in the first section.

    Sockets and Ports -- Doing it the Python way

    Sockets and ports form the core of any network oriented application. According to the formal definition a socket is “An endpoint of communication to which a name may be bound.” The concept (as well as implementation) comes from the BSD community. The 4.3BSD implementation defines three domains for the sockets:

    Unix Domain/ File-system Domain

    The sockets under this domain are used when two or more processes within a system have to communicate with each other. In this domain, the sockets are created within the file system. They are represented as strings that contain local path such as /var/lock/sock or /tmp/sock.

    Internet Domain

    This domain represents the processes that communicate over the TCP/IP. The sockets created for this domain are represented using a (host, port) tuple. Here a host is a fully qualified Internet host name that can be represented using a string or in the dotted decimal format (IP address).

    NS Domain

    This domain is the one used by the processes communicating over Xerox protocol which is now obsolete.

    Of these only the first two are commonly used. Python supports all of these. My discussion will be limited to the Internet domain. The following are the steps necessary for creating an application that uses TCP/IP sockets:

    1. Creating a socket
    2. Connecting the socket
    3. Binding the socket to an address
    4. Listening and accepting connections
    5. Transferring data/receiving data.

    But before creating a socket, libraries have to be imported. The socket module contains all that is needed to work with sockets. The imports can be done in two ways: import socket or from socket import *. If the first form is used, then to access the methods of socket module, socket.methodname() would have to be used. If the latter form is used, then the methods could be called without the fully qualified name. I will be using the second form for clarity of the code and ease. Now let's see the various provisions within the socket module for the programmers.

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

     
    Accelerating Trading Partner Performance
     
    Competing on Analytics
     
    Cost Effective Scaling with Virtualization and Coyote Point Systems
     
    Five Checkpoints to Implementing IP Telephony
     
    Hosted Email Security: Staying Ahead of New Threats
     




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