import socket
server = socket.socket()
server.bind(('10.7.156.60', 7080))
server.listen(512)
print('開始監(jiān)聽')
while True:
#接收客戶端請求
conversation, addr = server.accept()
print('===')
print(addr)
print('++++')
while True:
#接收消息
message_re = conversation.recv(1024).decode('utf-8')
print(message_re)
print('\\\\\\\\')
#服務(wù)器給客戶端發(fā)送消息
#1.服務(wù)器給客戶端發(fā)送一張圖片速梗,客戶端保存圖片到本地
if message_re == '1':
print('\\\\')
with open('./files/擔(dān)擔(dān)面.jpg', 'br') as f:
while True:
content = f.read()
if not content:
print('發(fā)送完畢')
conversation.send('發(fā)送完畢'.encode(encoding='utf-8'))
break
conversation.send(content)
# 關(guān)閉連接
# conversation.close()
#2.服務(wù)器輸入一段文字發(fā)送給客戶端滑肉, 客戶端將文字保存在一個(gè)message.txt文件中
elif message_re == '2':
content1 = input('請輸入傳輸文字:')
conversation.send(content1.encode(encoding='utf-8'))
# conversation.close()
#3.通知服務(wù)器關(guān)閉連接包券,并且客戶端結(jié)束
elif message_re == '3':
conversation.close()
print('斷開連接')
break
client:
import socket
client = socket.socket()
client.connect(('10.7.156.60', 7080))
with open('./files/show_page.txt', 'r', encoding='utf-8') as f:
page = f.read()
while True:
print(page)
# 給服務(wù)器發(fā)送消息
message = input('請輸入你想要的序號:')
client.send(message.encode(encoding='utf-8'))
if message == '1':
data = bytes()
re_data = client.recv(1024)
while re_data:
data += re_data
re_data = client.recv(1024)
if re_data == '發(fā)送完畢'.encode(encoding='utf-8'):
print('接收成功')
break
with open('./files/new_pic.jpg', 'bw') as f:
f.write(data)
print('接收到圖片啦')
continue
elif message == '2':
messages = client.recv(1024).decode('utf-8')
with open('./files/message.txt', 'w', encoding='utf-8') as f:
f.write(messages)
print('接收到文檔啦')
continue
elif message == '3':
print('中斷連接')
break
else:
print('請輸入1~3')
continue