ITxiaobaibai 2020-07-26
#服务器端import socketserver = socket.socket()server.bind((‘localhost‘,6969))#绑定要监听端口server.listen()#监听conn, addr = server.accept() # 等电话打进来# conn就是客户端连过来而在服务器端为其生成的一个连接实例print(conn, addr)while True: print(‘电话来了‘) data=conn.recv(1024) print(‘recv:‘,data) conn.send(data.upper())server.close()
#客户端import socketclient= 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(1024) print(‘recv:‘,data.decode())client.close()