tcp协议服务端
# coding=utf-8
import socket
s=socket.socket(socket.AF_INET,socket.SOCK_STREAM) # type: socket
s.bind(('',8888))
s.listen(1)
print ('服务器启动...')
conn,address=s.accept()
print (address)
data=conn.recv(1024)
print ('从客户端接收消息::{0}'.format(data.decode()))
conn.send('123'.encode())
conn.close()
s.close()
tcp协议客户端
# coding=utf-8
import socket
s=socket.socket(socket.AF_INET,socket.SOCK_STREAM)
s.connect(('127.0.0.1',8888))
s.send(b'hello')
data=s.recv(1024)
print ('从服务器端接收消息:{0}'.format(data.decode()))
s.close()
注意要首先启动服务端的代码在启动客户端代码