HomePython Page 4 - Sockets in Python: Into the World of Python Network Programming
Multi-Threaded Echo Server - Another Approach for Creating a Server - 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.
The exampl in the previous section uses the while loop to service different clients. For elucidations it is okay. But in the real world, it won’t work well. The reason is that more than one client cannot be served simultaneously with just while constructs. To overcome the limitations there are several strategies. One of them involves making the server multi-threaded. There are two parts to the creation of a multi threaded server:
1. Create threads for each accepted connections This is the core of the multi-threaded server. For each accepted connection request, a different thread is created and the serving of that particular client is carried out by an independent thread. Thus quick response times can be achieved.
2. Create a handler It is the handler where the whole processing goes on. In our case, this means transferring a file.
To make the echo server some changes need to be made. It starts with the accept part as shown below:
from socket import * from threading import * HOST = 'localhost' PORT = 21567 BUFSIZ = 1024 ADDR = (HOST, PORT) serversock = socket(AF_INET, SOCK_STREAM) serversock.bind(ADDR) serversock.listen(2) while 1: print 'waiting for connection…' clientsock, addr = serversock.accept() print '…connected from:', addr thread.start_new_thread(handler, (clientsock, addr))
After accepting the request, a new thread is created for the client. This is done for each connection request. The logic of handling the client is defined within the handler, which looks like this:
from socket import * from threading import * def handler(clientsock,addr): while 1: data = clientsock.recv(BUFSIZ) if not data: break clientsock.send(‘echoed:..’, data)
while 1: print 'waiting for connection…' clientsock, addr = serversock.accept() print '…connected from:', addr thread.start_new_thread(handler, (clientsock, addr)) #some other cleanup code if necessary
The handler has to be defined before calling it. The handler contains the same code that was contained in the inner while loop previously. This example is not optimized. But it serves the purpose of providing a different approach for serving multiple clients.
Here are a few points to keep in mind:
The low level sockets can be mixed and matched with other modules such as threads and forks to create a server capable of serving multiple clients simultaneously.
While using the threading approach, the locking and synchronization issues must be kept in mind.
Security measures must be taken when creating FTP servers.
This brings us to the end of this discussion. In the introductory section I mentioned flexibility as one of the core aspects of Python. Ability to work with low-level sockets is one of them. But at the other end of the spectrum are the pre-built yet extensible web servers. These will be discussed in the near future.