单进程单线程,完成并发服务器(基础版)

# 单进程单线程,完成并发服务器
from socket import *


def main():

    serverSocket = socket(AF_INET, SOCK_STREAM)
    serverSocket.bind(("", 7788))
    serverSocket.listen(5)

    serverSocket.setblocking(False)

    clientServerList = []

    while True:
        try:
            clientServer, clientAddr = serverSocket.accept()
        except Exception as e:
            pass
        else:
            clientServer.setblocking(False)
            print("客户端:%s已连接" % str(clientAddr))
            clientServerList.append((clientServer, clientAddr))

        for clientServer, clientAddr in clientServerList:
            try:
                recvData = clientServer.recv(1024)
            except Exception as e:
                pass
            else:
                if recvData:
                    print("%s:%s" % (str(clientAddr), recvData))
                else:
                    clientServer.close()
                    print("%s断开连接" % str(clientAddr))
                    clientServerList.remove((clientServer, clientAddr))


if __name__ == '__main__':
    main()

基础的版本,主要理解利用非堵塞的socket通信,非堵塞+for循环模拟多线程

注:
测试使用网络调试工具就可以,这个可以多开。
代码&工具下载地址:
下载地址1
下载地址2

你可能感兴趣的:(python)