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':
# 發(fā)送圖片
print('發(fā)送圖片')
with open('./files/luffy.jpeg', 'br') as f:
content = f.read()
conversation.send(content) # 發(fā)送圖片信息
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. 通知結(jié)束
==========================="""
while True:
client = socket.socket()
client.connect(('10.7.156.97', 8082))
print(page)
value = input('請選擇(1-3):')
# 發(fā)送數(shù)據(jù)
client.send(value.encode('utf-8'))
# 接收服務(wù)器返回的數(shù)據(jù)
if value == '1':
data = bytes()
message_re = client.recv(1024)
while message_re:
print('接收到圖片')
data += message_re
message_re = client.recv(1024)
# 關(guān)閉連接
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()
最后編輯于 :
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者