/Network programming in Python

Network programming in Python

This article does not contain a horrible content as the title seems to be, I’m just writing some hints, like a tutorial for creating some TCP and UDP connections, an important part in Network Programming. I’ll not cover the TCP and UDP concepts, neither the sockets definition, etc.You can skim on a TCP/IP book like Understanding TCP/IP or the TCP/IP guide for more info on that. I’ll show you how things go in Python only.Don’t expect a huge hard code also, everything is simple and naive.

Assuming that you know all about client-server architecture, sockets, and networking, let us try to bring this concept to Python.
The primary module we will be using is “socket”. The socket() function is used to create the socket objects, and this is the first one we will be tackling now.

To create a TCP/IP socket, you call socket.socket() like this:

As you know [If you don’t, don’t be sad, take a look at this page], socket() takes some parameters, the first first is the “Socket Family”, the second is “Socket Type” and the third is the “Protocol” which is usually defaulted to 0.

The same is for UDP sockets:

Since there are numerous socket module attributes, this is one of the exceptions where using “from module import *” is acceptable because of the number of module attributes. If we applied “from socket import *“, we bring the socket attributes into our namespace. so you will be writing

Now, let’s go and create a simple TCP server.
First, keep in mind that this is one of the available ways to design your server. We will involve some pseudo code to operate the way you want it to:

serverSocket = socket() #create server socket
serverSocket.bind() #bind socket to address
serverSocket.listen() #listen for connection
infinite_loop: #server infinite loop
clientSocket = serverSocket.accept() #accept client connection
comm_loop: #communication loop
clinetSocket.receive() / clientSocket.send() #send/receive
clinetSocket.close() #close client socket
serverSocket.close() #close server socket

 

Because TCP is a connection oriented system, TCP servers must listen for incoming connections. Once this step is complete, a server can start its infinite loop.

A simple single threaded server will wait for an incoming connection. Take in mind that accept() is blocking, it means, the execution is suspended until a connection arrives. [See note 1, down]

Once a connection is accepted, a separate client socket is returned by accept() for the upcoming message interchange.

The operator now can resume waiting for new calls.When an incoming request arrives, a new communication port is created to converse directly with the client while the main one is free to accept new client connections. [See note 2, down]

Let’s start writing a naive TCP server and client:

After running our code, The “…connected from…” message was received when our client made its connection. The server went back to wait for new clients while we continued receiving “service.” When we exited from the server, we had to break out of it, resulting in an exception. The best way to avoid such an error is to create a more graceful exit, as we have been discussing. [See note 3, down]

Creating a UDP server and client:
Creating a UDP server does not require as much as TCP. They are not connection oriented.
Let’s see a naive code for implementing UDP server and client.

The significant difference is that there’s no “handing off” of a client connection to a separate socket for succeeding communication. and this is a UDP client:

Running the code of both, you should see something like that regarding the client:

> hi
[Thu Dec 16 13:35:06 2010] hi
>

 

and for the server :

Waiting for message...
received from, returned to: (u'127.0.0.1', 48852)
Waiting for message...

 

NOTES

1: Sockets do support a non-blocking mode; refer to the documentation or operating systems textbooks for more details on why and how you would use non-blocking sockets.

2: The SocketServer module, a high-level socket communication module written on top of socket, supports both threaded and spawned process handling of client requests.

3: One way to create this “friendly” exit is to put the server’s while loop inside the except clause of a try-except statement and monitor for EOFError or KeyboardInterrupt exceptions. Then in the except clause, you can make a call to close the server’s socket.