server
import socket
server = socket.socket()
server.bind(('10.7.156.97', 8082))
server.listen(512)
while True:
conversation, addr = server.accept()
message_re = conversation.recv(1024).decode('utf-8')
if message_re == '1':
# 发送图片
print('发送图片')
with open('./files/luffy.jpeg', 'br') as f:
content = f.read()
conversation.send(content) # 发送图片信息
conversation.close()
elif message_re == '2':
message = input('>>>')
conversation.send(message.encode('utf-8'))
conversation.close()
client
import socket
number = 0
page = """
===========================
1. 需要图片
2. 需要文字
3. 通知结束
==========================="""
while True:
client = socket.socket()
client.connect(('10.7.156.97', 8082))
print(page)
value = input('请选择(1-3):')
# 发送数据
client.send(value.encode('utf-8'))
# 接收服务器返回的数据
if value == '1':
data = bytes()
message_re = client.recv(1024)
while message_re:
print('接收到图片')
data += message_re
message_re = client.recv(1024)
# 关闭连接
client.close()
number += 1
with open('./new%d.jpeg'%number, 'wb') as f:
f.write(data)
elif value == '2':
message_re = client.recv(1024)
print(message_re.decode('utf-8'))
# client.close()
网友评论