Sockets in Python: Into the World of Python Network Programming - Multi-Threaded Echo Server - Another Approach
(Page 4 of 4 )
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))
serversock.close()
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)
clientsock.close()
if __name__==’__main__’:
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))
#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.
Parting Thoughts
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.
| DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware. |