HomePython Page 3 - Sockets in Python: Into the World of Python Network Programming
A Simple Echo 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.
Now, to make it listen for incoming requests continuously, place the accept() method in a while loop. This is not the most preferred mode. The preferred way will be discussed in the next section:
while 1: print 'waiting for connection…' clientsock, addr = serversock.accept() print '…connected from:', addr : :
Next, receive the data from the client and echo it back. This has to continue until the client doesn’t send the null data or ctrl+c. To achieve this, use a while loop again and then close the connection when done.