Python
  Home arrow Python arrow Page 2 - Sockets in Python
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
By: Peyton McCullough
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: starstarstarstarstar / 102
    2004-12-28


    Table of Contents:
  • Sockets in Python
  • The Basics
  • Connecting to the Server
  • Sockets...Simplified
  • Summary

  • 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 - The Basics
    ( Page 2 of 5 )

    Using sockets in Python is quite easy. Open up your Python command line, and let’s get to some code.

    The first thing we must do is import the socket library:

    >>> import socket

    The socket library contains all the tools we need to work with sockets. The next thing we need to do is create a socket. This is simple. Execute the following code:

    >>> mySocket = socket.socket ( socket.AF_INET, socket.SOCK_STREAM )

    This creates a stream socket. You can also work with datagrams by replacing SOCK_STREAM with SOCK_DGRAM. A socket stream is where there is a constant connection between the client and server that stays alive until it is closed, and both the client and the server know if the connection is still alive. With datagrams, however, that is not the case. The connection is not kept alive, and your data might not even be received. Although datagrams can sound like a bad idea at first, they have their purposes. It might be easier and faster to use datagrams in certain situations.

    Writing a Simple Server

    Let’s write a simple server. If you’ll remember, a server is anything that receives connections from other computers, clients. Create a new Python file named server.py and insert the following code into it:

    import socket
    mySocket = socket.socket ( socket.AF_INET, socket.SOCK_STREAM )
    mySocket.bind ( ( '', 2727 ) )
    mySocket.listen ( 1 )
    while True:
       channel, details = mySocket.accept()
       print 'We have opened a connection with', details
       print channel.recv ( 100 )
       channel.send ( 'Green-eyed monster.' )
       channel.close()


    That’s quite a mouthful, so let’s split it up into something we can understand. The first two lines should already look familiar. We create a new socket to use. In the third line, we open port 2727 for connections.

    To understand what a port is, let’s go back to our analogy. Picture the pump machine with thousands of pipes leading in and out of it. Each pipe would be a port, and clients would have the option of connecting to different ports. However, each port would be different – some might pump out green water, and others might pump out orange water.

    The next line tells our socket to wait, or listen, for clients. Following that, there is an infinite loop. In this loop, we accept client connections, print out the client's address, print out the client's message and finally send a message back, closing the connection when we're done.



     
     
    >>> More Python Articles          >>> More By Peyton McCullough
     

       

    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