服务器
import socket
server = socket.socket()
server.bind(('10.7.181.75', 8885))
server.listen(512)
while True:
print('开始监听....')
connect, address = server.accept()
print('接受请求')
print(connect, address)
while True:
#接收信息
recv_data = connect.recv(1024)
print('接收:', recv_data.decode(encoding='utf-8'))
#发送信息
message = str(recv_data.decode(encoding='utf-8'))
connect.send(message.encode())
if recv_data.decode(encoding='utf-8') == 'b':
names = ['anchor', 'bear', 'bucket', 'ghost', 'hat', 'hat1', 'hat2', 'logo', 'luffy', 'new', 'rudder', 'sun', 'tel']
for x in names:
x_b = x.encode()
connect.send(x_b)
re_pic = connect.recv(1024)
print(re_pic)
photo = re_pic.decode()
with open('files/tel.png', 'rb') as f:
connect.send(f.read())
客户端
import socket
import client as client
client = socket.socket()
client.connect(('10.7.181.75', 8885))
choice = input('a.请求文字信息:'
'b.请求图片信息:')
client.send(choice.encode())
rv = client.recv(1024)
if rv.decode(encoding='utf-8') == 'a':
#请求文字信息
while True:
send_message = input('请输入消息(客户端):')
client.send(send_message.encode())
recv_data = client.recv(1024)
print(recv_data.decode(encoding='utf-8'))
elif rv.decode(encoding='utf-8') == 'b':
while True:
re_data = client.recv(1024)
print(re_data.decode(encoding='utf-8'))
if re_data.decode(encoding='utf-8') == 'tel':
pic = input('请选择一张图片:')
client.send(pic.encode())
# while True:
# new_pic = client.recv(1024)
# with open('new_files/new.png', 'ab') as f:
# f.write(new_pic)
#
# if (len(new_pic) < 1024):
# break