"""__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('开始监听')
connection, address = server.accept()
print('接收到连接请求,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('请求处理完成')
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('该访问已断开')
break
else:
connection.send('不存在该指令,请重新输入!'.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()