Python
  Home arrow Python arrow Page 2 - 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? 
Google.com  
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 - Sockets Step-by-Step
    ( Page 2 of 4 )

    1. Creating a socket
    A socket can be created by making call to the socket() function. The socket() function returns a socket in the domain specified. The parameters to the function are:

    • family: The family parameter specifies in which domain the socket has to be created.  The valid values are AF_UNIX for the UNIX domain and AF_INET for the Internet domain.
    • type: Type defines the type of the protocol to be used. The type can be connection-oriented like TCP or connection-less like UDP. These are defined by the constants SOCK_STREAM for TCP, SOCK_DGRAM for UDP. Other valid parameters are SOCK RAW, SOCK SEQPACKET and SOCK RDM.
    • protocol: This is generally left at the default value. The default is 0.

    So a socket for Internet domain is created thus:

    testsocket=socket(AF_INET,SOCK_STREAM)
     
    2. Connecting the Socket
    Sockets thus created can be used on the server-side or client-side. To use the socket on the client side, it needs to be connected to a host. That can be done using the connect() method of the socket object. The connect() method accepts either the host name as the parameter or a tuple containing host name/address and port number as parameter. For example, to connect to a host whose address is 192.168.51.100 and the port number 8080 the statement would be:

    testsocket.connect((‘192.168.51.100’,8080))

    3. Binding the socket to an address
    If the socket has to be used on the server side, then the socket has to be bound to an address and a port, thus naming it. To bind a socket to an address, the bind() method of the socket object has to be used. The valid parameter is a tuple containing the address to which the socket has to be bound and the port at which it has to listen for incoming requests. To use the same socket, i.e. testsocket, on the server side the statement would be:

    testsocket.bind((‘192.168.51.100’,8080))

    4. Listening and accepting connections
    Once a socket has been named, it then must be instructed to listen at the given port for incoming requests. This can be done using the listen() method. The listen accepts a number representing the maximum queued connection. The argument should be at least 1. For example the following code sets the max queued connection to 2:

    testsocket.listen(2)

    The next thing to be done is to accept the incoming connection requests. This can be accomplished with the accept() function. This function returns a tuple containing a new socket object representing the client and the address of the client. For example:

    clientsock,address= testsocket.accept()

    In the above statement clientsock would contain a new socket object and address would contain the address of the client.

    5. Transferring data/receiving data
    Data can be transferred using the recv() and send() methods of the socket object. Socket’s recv() method is used to receive the data send from the server or from the client. The parameters are a buffer size for the data, and flags. The flags parameter is optional. So to receive data the code would be:

    buff=1024
    testsocket.recv(buff)

    To send the data, a call to the send method is in order. The parameters are the data to be sent and the flags. To elucidate  further:

    data=raw_input(‘>>’)
    testsocket.send(data)



     
     
    >>> 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
    For more Enterprise Application Development news, visit eWeek