"""__author__ = iwsyt"""
from socket import *
from os import listdir
from json import *
server = socket()
ip = '10.7.181.87'
port = 1126
server.bind((ip, port))
server.listen(1024)
while True:
print('開(kāi)始監(jiān)聽(tīng)')
connection, address = server.accept()
print('接收到連接請(qǐng)求君珠,ip地址:{}格粪,端口:{}'.format(address[0], address[1]))
while True:
print('正在等待操作...')
send_meg = 'Please select a request category:\n1.Request text message\n2.Request image\n3.exit'
connection.send(send_meg.encode(encoding='utf-8'))
recv_data = connection.recv(1024)
print('正在處理操作...')
recv_meg = str(recv_data, 'utf-8')
if recv_meg == '1':
connection.send(recv_data)
print('請(qǐng)求處理完成')
elif recv_meg == '2':
img = listdir(r'E:\Wallpaper')
connection.send((dumps(img).encode('utf-8')))
index = int(connection.recv(1024).decode('utf-8'))-1
img_name = img[index]
connection.send(img_name.encode('utf-8'))
with open(r'E:\Wallpaper\{}'.format(img_name), 'rb') as f:
connection.send(f.read())
print(connection.recv(1024).decode('utf-8'))
connection.send('傳輸完成'.encode('utf-8'))
elif recv_meg == '3':
print('該訪問(wèn)已斷開(kāi)')
break
else:
connection.send('不存在該指令唠亚,請(qǐng)重新輸入蔓搞!'.encode('utf-8'))
"""__author__ = iwsyt"""
from socket import *
from json import *
client = socket()
ip = '10.7.181.87'
port = 1126
client.connect((ip, port))
while True:
recv_data = client.recv(1024)
print(1)
print(recv_data.decode(encoding='utf-8'))
send_meg = input('\nPlease choose(1 or 2):')
client.send(send_meg.encode(encoding='utf-8'))
if send_meg == '1':
print(client.recv(1024).decode('utf-8'))
elif send_meg == '2':
f = open('files/img_info.txt', 'w', encoding='utf-8')
f.close()
while True:
img_info = client.recv(1024).decode('utf-8')
with open('files/img_info.txt', 'a', encoding='utf-8') as f:
f.write(img_info)
if len(img_info) < 1024:
break
with open('files/img_info.txt', 'r') as f:
content = loads(f.read())
for index in range(len(content)):
print(str(index + 1) + '.' + content[index])
send_meg = input('\nPlease choose img by num:')
client.send(send_meg.encode('utf-8'))
print('img downloading...')
img_name = client.recv(1024).decode('utf-8')
while True:
img_data = client.recv(1024)
with open('files/{}'.format(img_name), 'ab') as f:
f.write(img_data)
if len(img_data) < 1024:
print('img download complete!')
break
client.send('接收成功'.encode('utf-8'))
print(client.recv(1024).decode('utf-8'))
elif send_meg == '3':
break
else:
print(client.recv(1024).decode('utf-8'))
client.close()
最后編輯于 :
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者