106-Time message server based on multi-process

106-Time message server based on multi-process

1. Supports simultaneous access by multiple clients 2. After the client sends a message to the server, the server sends the message back to the client with time 3. Each client disconnects will generate a zombie process, and destroys all the new clients when they connect Zombie process

import socket
import os
from time import strftime

class TcpTimeServer:
    def __init__(self, host='', port=12345):
        self.addr = (host, port)
        self.serv = socket.socket()
        self.serv.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
        self.serv.bind(self.addr)
        self.serv.listen(1)

    def chat(self, c_sock):
        while True:
            data = c_sock.recv(1024)
            if data.strip() == b'quit':
                break
            data ='[%s] %s'% (strftime('%H:%M:%S'), data.decode('utf8'))
            c_sock.send(data.encode('utf8'))
        c_sock.close()

    def mainloop(self):
        while True:
            cli_sock, cli_addr = self.serv.accept()
            pid = os.fork()
            if pid:
                cli_sock.close()
                while True:
                    result = os.waitpid(-1, 1)[0] # waitpid will give priority to zombie processes
                    if result == 0:
                        break
            else:
                self.serv.close()
                self.chat(cli_sock)
                exit()

        self.serv.close()

if __name__ =='__main__':
    s = TcpTimeServer()
    s.mainloop()
Reference: https://cloud.tencent.com/developer/article/1334517 106-time message server based on multi-process-cloud + community-Tencent Cloud