day08_03 客户端多次连接

socket_server.py

import socketserver


class MyTCPHandler(socketserver.BaseRequestHandler):

    def handle(self):
        while True:
            try:
                self.data = self.request.recv(1024).strip()
                print("{} wrote:".format(self.client_address[0]))
                print(self.data)
                self.request.send(self.data.upper())
            except ConnectionResetError as e:
                print("err", e)
                break


if __name__ == "__main__":
    # 允许用户多次连接
    HOST, PORT = "localhost", 6969
    # Create the server, binding to localhost on port 9999
    server = socketserver.ThreadingTCPServer((HOST, PORT), MyTCPHandler)
    server.serve_forever()

socket_client.py

import socket

__author__ = "Alex Li"


client = socket.socket()  # 声明socket类型,同时生成socket连接对象
client.connect(('localhost', 6969))

while True:
    msg = input(">>:").strip()
    if len(msg) == 0:
        continue
    client.send(msg.encode("utf-8"))
    data = client.recv(10240)
    print("客户端接收到服务器端,转换后的数据:", data.decode())

# 客户端关闭时,服务器dau端也会收到心跳检测失效
client.close()
上一篇:【Leetcode刷题笔记 持续更新】Day08


下一篇:C#两个时间的时间差的方法