1、服务端
import socket
HOST = ''
PORT = 9001
ADDR = (HOST,PORT)
BUFFSIZE = 1024
MAX_LISTEN = 5
def tcpServer():
#tcp服务
#with socket.socket() as s:
with socket.socket(socket.AF_INET,socket.SOCK_STREAM) as s:
#绑定服务器地址和端口
s.bind(ADDR)
#启动服务监听
s.listen(MAX_LISTEN)
print('等待用户接入。。。。。。。')
while True:
#等待客户端连接请求,获取connSock
conn,addr = s.accept()
print('警告,远端客户:{}接入系统!!!'.format(addr))
with conn:
while True:
print('接收请求信息。。。。。。')
#接收请求信息
data = conn.recv(BUFFSIZE)
print('data = %s' % data)
print('接收数据:{!r}'.format(data.decode('utf-8')))
#发送请求数据
conn.send(data.encode('utf-8'))