Python
  Home arrow Python arrow Page 2 - 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 
Moblin 
JMSL Numerical Library 
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 / 34
    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 - 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


       · 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 2 hosted by Hostway