HomePython 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.
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:
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:
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:
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:
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:
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:
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: