码迷,mamicode.com
首页 > 其他好文 > 详细

socketserver实现并发

时间:2018-09-03 02:30:40      阅读:150      评论:0      收藏:0      [点我收藏+]

标签:bre   ever   tin   __name__   def   enc   客户   exce   client   

服务器端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实现并发

标签:bre   ever   tin   __name__   def   enc   客户   exce   client   

原文地址:https://www.cnblogs.com/kuraki/p/9575841.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!