Home arrow Python arrow Page 2 - Sockets in Python: Into the World of Python Network Programming

Sockets Step-by-Step - Python

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.

TABLE OF CONTENTS:
  1. Sockets in Python: Into the World of Python Network Programming
  2. Sockets Step-by-Step
  3. A Simple Echo Server
  4. Multi-Threaded Echo Server - Another Approach
By: A.P.Rajshekhar
Rating: starstarstarstarstar / 62
November 28, 2005

print this article
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement

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
 

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort
   

PYTHON ARTICLES

- Python Big Data Company Gets DARPA Funding
- Python 32 Now Available
- Final Alpha for Python 3.2 is Released
- Python 3.1: String Formatting
- Python 3.1: Strings and Quotes
- Python 3.1: Programming Basics and Strings
- 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

Developer Shed Affiliates

 


Dev Shed Tutorial Topics: