#server.py
from socket import *
import os
path = 'files\\'
server = socket()
server.bind(('192.168.202.1', 12345))
server.listen(52)
while True:
connect, address = server.accept()
while True:
message = connect.recv(1024).decode(encoding='utf-8')
if message == 'a':
connect.send((input('请输入要传输的文字信息:')).encode())
elif message == 'b':
list1 = os.listdir(path)
connect.send(str(list1).encode())
index = (connect.recv(1024).decode(encoding='utf-8'))
file_path = os.path.join(path, list1[int(index) - 1])
with open(file_path, 'rb')as f:
connect.send(f.read())
#client.py
from socket import *
import datetime
def main_window():
print('================================')
print('a.请求文字信息')
print('b.请求图片信息')
print('q.退出连接')
client = socket()
client.connect(('192.168.202.1', 12345))
main_window()
choice = input('\n请选择:')
while True:
if choice == 'a':
client.send(choice.encode())
re_data = client.recv(1024)
if re_data.decode(encoding='utf-8'):
print('\n文字信息:',re_data.decode(encoding='utf-8'))
print('传输成功!\n')
else:
print('传输失败!\n')
main_window()
choice = input('\n请选择:')
elif choice == 'b':
client.send(choice.encode())
print('')
dir_1ist = eval(client.recv(1024).decode(encoding='utf-8'))
for i in range(len(dir_1ist)):
print(str(i+1) + ': ' + dir_1ist[i])
print('')
client.send(input('请输入序号:').encode())
print('\n传输中……')
while True:
re_data = client.recv(1024)
nowTime = str(datetime.datetime.now().strftime("%Y%m%d%H%M%S"));
with open('files\\'+nowTime+'.jpg','ab')as f:
f.write(re_data)
if len(re_data) < 1024:
print('\n传输完毕!')
break
main_window()
choice = input('\n请选择:')
elif choice == 'q':
client.close()
break
else:
print('输入错误,重新输入!')
choice = input('\n请选择:')