46

How do I make a simple Python echo server that remembers clients and doesn't create a new socket for each request? Must be able to support concurrent access. I want to be able to connect once and continually send and receive data using this client or similar:

import socket

sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
host = raw_input("Server hostname or ip? ")
port = input("Server port? ")
sock.connect((host,port))
while True:
    data = raw_input("message: ")
    sock.send(data)
    print "response: ", sock.recv(1024)

I.e. with the server running on port 50000, using the above client I want to be able to do this:

me@mine:~$ client.py
Server hostname or ip? localhost
Server Port? 50000
message: testa
response: testa
message: testb
response: testb
message: testc
response: testc
nettux
  • 5,270
  • 2
  • 23
  • 33

1 Answers1

98

You can use a thread per client to avoid the blocking client.recv() then use the main thread just for listening for new clients. When one connects, the main thread creates a new thread that just listens to the new client and ends when it doesn't talk for 60 seconds.

import socket
import threading

class ThreadedServer(object):
    def __init__(self, host, port):
        self.host = host
        self.port = port
        self.sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        self.sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
        self.sock.bind((self.host, self.port))

    def listen(self):
        self.sock.listen(5)
        while True:
            client, address = self.sock.accept()
            client.settimeout(60)
            threading.Thread(target = self.listenToClient,args = (client,address)).start()

    def listenToClient(self, client, address):
        size = 1024
        while True:
            try:
                data = client.recv(size)
                if data:
                    # Set the response to echo back the recieved data 
                    response = data
                    client.send(response)
                else:
                    raise error('Client disconnected')
            except:
                client.close()
                return False

if __name__ == "__main__":
    while True:
        port_num = input("Port? ")
        try:
            port_num = int(port_num)
            break
        except ValueError:
            pass

    ThreadedServer('',port_num).listen()

Clients timeout after 60 seconds of inactivity and must reconnect. See the line client.settimeout(60) in the function ThreadedServer.listen()

nettux
  • 5,270
  • 2
  • 23
  • 33
  • 1
    I found this interesting, short question is the line self.sock.listen(5) responsible for the amount of allowed connections? So basically I can accept 5 clients at once but not more or what is the 5 good for? – Kev1n91 Apr 14 '17 at 15:13
  • 1
    @Kev1n91 The 5 is the backlog argument which specifies how many connections can be queued up waiting to be accepted. Explained in the docs here: https://docs.python.org/2/library/socket.html#socket.socket.listen – nettux Apr 14 '17 at 15:38
  • `Traceback (most recent call last): File "E:/myProto/threadedserver.py", line 36, in ThreadedServer('',port_num).listen() File "E:/myProto/threadedserver.py", line 10, in __init__ self.sock.bind((self.host, self.port)) TypeError: an integer is required (got type str)` – cascading-style May 03 '17 at 17:56
  • 1
    @cascading-style I wrote this in python 2.7 and I'm guessing you're using python 3? `input` returns a string in python 3 - just make `port_num` an int: `port_num = int(port_num)` – nettux May 03 '17 at 18:02