socketserver

Posted yangwenyu-6

tags:

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

客户端

import socket

client = socket.socket()
client.connect((‘127.0.0.1‘, 8001))

while 1:
    msg = input(‘客户端说>>>‘)
    client.send(msg.encode(‘utf-8‘))

    from_server_msg = client.recv(1024)
    print(from_server_msg.decode(‘utf-8‘))

  

服务端

import socketserver

class Method(socketserver.BaseRequestHandler):
    def handle(self):
        while 1:
            from_client_msg = self.request.recv(1024)  # self.request = conn
            print(from_client_msg.decode(‘utf-8‘))
            msg = input(‘服务器说:‘)
            self.request.send(msg.encode(‘utf-8‘))

if __name__ == ‘__main__‘:
    ip_port = (‘127.0.0.1‘, 8001)
    server = socketserver.ThreadingTCPServer(ip_port,Method)
    server.serve_forever()

  

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

socketserver.py代码阅读笔记

socketserver.py代码阅读笔记

socketserver实现并发

socketserver

Python网络编程篇之socketserver

socketserver源码解析和协程版socketserver