socket并发聊天

服务端:

import socketserver

class MyServer(socketserver.BaseRequestHandler):
def handle(self):
print('服务端启动')
while True:
conn = self.request
print(self.client_address)
while True:
client_data = conn.recv(1024)
print(str(client_data,'utf-8'))
print('waiting')
inp = input('>>>')
conn.sendall(bytes(inp,'utf-8'))
conn.close()
if __name__=='__main__':
server=socketserver.ThreadingTCPServer(('127.0.0.1',8000),MyServer)
server.serve_forever()

客户端:
import socket

sk=socket.socket()
address = ('127.0.0.1',8000)
sk.connect(address)


while True:
inp = input('>>>')
sk.sendall(bytes(inp,'utf-8'))
if inp=='exit':
break

data = sk.recv(1024)
data = str(data,'utf-8')
print(data)
sk.close()


posted @ 2018-03-18 20:02  飘零0  阅读(198)  评论(0编辑  收藏  举报