socketserver实现并发

Posted kuraki

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了socketserver实现并发相关的知识,希望对你有一定的参考价值。

服务器端sockforever版

import socketserver


class MyServer(socketserver.BaseRequestHandler):
def handle(self): # print(‘conn is: ‘,self.request) print(address is:,self.client_address) while True: try: msg = self.request.recv(1024) if not msg:break print(收到客户端的消息: ,msg.decode(utf-8),self.client_address) self.request.send(msg) except Exception as e: print(e) break if __name__ == __main__: server = socketserver.ThreadingTCPServer((127.0.0.1,8080),MyServer) server.serve_forever()

client端

import socket


buffer_size=1024
sk = socket.socket()
sk.connect((127.0.0.1,8080))

while True:
    msg = input(>>:)
    if not msg:continue
    if msg == quit:break
    sk.send(msg.encode(utf-8))
    data =sk.recv(buffer_size)
    print(来自客户端的消息: ,data.decode(utf-8))

sk.close()

 


以上是关于socketserver实现并发的主要内容,如果未能解决你的问题,请参考以下文章

socketserver模块(实现并发)

socketserver实现并发

socketserver实现并发

python,利用socketserver模块实现并发聊天

基于socketserver实现并发

Python之路 - Socketserver实现多并发