Python
  Home arrow Python arrow 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
    ( 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
     

       

    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 6 hosted by Hostway
    Stay green...Green IT