Day19 - 作业

创建一个服务器端,能够同时接收多个客户端的请求:

服务器端:

from socket import *
from threading import *

class Thread1(Thread):
    def __init__(self):
        super().__init__()

    def run(self) -> None:
        print('==========')
        while True:
            connect, address = server.accept()
            while True:
                data = connect.recv(1024000)
                print(data.decode(encoding='utf-8'))
                connect.send(data)
        connect.close()


server = socket()
server.bind(('10.7.181.89', 6688))
server.listen(100000)
if __name__ == '__main__':
    p1 = Thread1()
    p2 = Thread1()

    p1.start()
    p2.start()

客户端1:

from socket import *

client = socket()
client.connect(('10.7.181.89', 6688))
while True:
    message = input('请输入:')
    client.send(message.encode())
    re_data = client.recv(1024000)
    print(re_data.decode(encoding='utf-8'))
client.close()


客户端2:

from socket import *

client = socket()
client.connect(('10.7.181.89', 6688))
while True:
    message = input('请输入:')
    client.send(message.encode())
    re_data = client.recv(1024000)
    print(re_data.decode(encoding='utf-8'))
client.close()

你可能感兴趣的:(Day19 - 作业)