tcp示例
server:
# -*- coding:gb2312 -*- import socket import time if __name__ == "__main__": s=socket.socket(socket.AF_INET,socket.SOCK_STREAM) host='' port=12345 s.bind((host,port)) s.listen(5) while 1: print('wait-----') t,addr=s.accept() message=t.recv(1024) message=bytes.decode(message) if message != 'time': print("message error") continue secs=time.ctime() reply=str.encode(secs) t.send(reply) print("succeed")
client:
# -*- coding=gb2312 -*- import socket if __name__ == "__main__": s=socket.socket(socket.AF_INET,socket.SOCK_STREAM) address=('127.0.0.1',12345) s.connect(address) data=b'time' s.sendall(data) buf=s.recv(1024) buf=bytes.decode(buf) print(buf)