Python 基于C/S模式的文件上传和下载

本人新人一枚,如果程序有不足,请多多指教

 # password.log  # 这个文件是存储用户名和密码的,用来认证client用户的。

admin|admin123

123|123

# server:

fileabs_storage = 'D:\storage'  #client上传文件存储路径

count = 'admin'
def get_md5(pwd):
md5 = hashlib.md5(count.encode('utf-8'))
md5.update(pwd.encode('utf-8'))
ret = md5.hexdigest()
return ret

def file_user():
with open('password.log','r',encoding='utf-8') as f:    # password.log是存储用户名和密码的文件
for item in f:
username,password = item.strip().split('|')
dic = {'username':username,'password':get_md5(password)}
if dic[ 'username' ] == user_name [ 'username' ] and dic[ 'password' ] == user_name ['password' ] :
return {'result' : True}
else:
continue
return {'result' : False}

def receive():
byte_file_name = conn.recv(1024).decode('utf-8')
file_name_1 = json.loads(byte_file_name)

with open(os.path.join(fileabs_storage,file_name_1['filename']) ,'wb') as f:
while True:
file_receive = conn.recv ( file_name_1 [ 'file_size' ] )
if file_receive:
f.write(file_receive)
else:
print ( '\033[32mupload--- \033[0m' + os.path.join(fileabs_storage,file_name_1['filename'] ))
break

def send_file():
result = os.walk(fileabs_storage)
file_list = []
for a,b,c in result:
for item in c:
path = os.path.join(a,item)
dire = os.path.dirname(path)
file = os.path.basename(path)
size = os.path.getsize(path)
dic = {'file_dire':dire,'file_name':file,'file_size':size}
file_list.append(dic)
byte_list = json.dumps(file_list)
conn.send(byte_list.encode('utf-8'))
# print(path)
byte_file_name = conn.recv(1024).decode('utf-8')
print('\033[32mdownload-- \033[0m'+byte_file_name)
if os.path.isabs(byte_file_name):
conn.send ( 'True'.encode ( 'utf-8' ) )
with open(byte_file_name,'rb') as f:
while True:
name = f.read(20480000)
if name:
conn.send(name)
else:break
else:conn.send('False'.encode('utf-8'))



sk = socket.socket()
sk.bind(('127.0.0.1',9005))
sk.listen()
while True:
conn,addr = sk.accept()


user_attest = conn.recv(1024).decode('utf-8')
user_name = json.loads(user_attest)

byte_user_true = json.dumps ( file_user ( ) )
conn.send ( byte_user_true.encode ( 'utf-8' ) )
if file_user()['result']:
print('client已上线:')
byte_load = conn.recv(1024).decode('utf-8')
if byte_load == 'upload':
receive()
if byte_load == 'download':
send_file()

conn.close()
sk.close()

#client:


import os
import json
import socket
import hashlib


count = 'admin'
def get_md5(pwd):
md5 = hashlib.md5(count.encode('utf-8'))
md5.update(pwd.encode('utf-8'))
ret = md5.hexdigest()
return ret

def upload(file):
if os.path.isabs(file):
file_name = os.path.basename(file)
file_size = os.path.getsize(file)
file_dic = {'filename':file_name,'file_size':file_size}
print('文件上传成功')
byte_file = json.dumps(file_dic)
sk.send(byte_file.encode('utf-8'))
# 方法一:
# with open(file,'rb') as f:
# seek_id = 0
# while True:
# f.seek(seek_id)
# admin = f.readline()
# if admin:
# sk.send(admin)
# seek_id = f.tell()
# else:break
# 方法二:
with open ( file, 'rb' ) as f :
while True:
user_name = f.read(20480000)
if user_name:
sk.send(user_name)
else:return

def receive_files(file_name,file_size):
with open(file_name,'wb') as f:
while True:
file_receive = sk.recv ( file_size )
if file_receive:
f.write(file_receive)
else:
print('文件下载成功')
return

def download():
file_path = False
file_name = sk.recv ( 1024 ).decode ( 'utf-8' )
file_name_dic = json.loads ( file_name )
print ( '\033[31m可下载的文件:\033[0m' )
for item in file_name_dic :
print ( '\033[32m%s\033[0m' % item['file_name'] )
file_transport = input ( 'file_download:' )
for item in file_name_dic:
if item['file_name'] == file_transport:
path = os.path.join(item['file_dire'],file_transport)

sk.send(path.encode('utf-8'))
ret_true = sk.recv(1024).decode('utf-8')

if ret_true == 'True':
print(item['file_size'])
receive_files(file_transport,item['file_size'])

def login():
user = input ( 'username:' )
pwd = input ( 'password:' )
dic = {'username' : user, 'password' : get_md5 ( pwd )}
byte_dic = json.dumps ( dic )
sk.send ( byte_dic.encode ( 'utf-8' ) )
byte_user_true = sk.recv(1024).decode()
user_true = json.loads(byte_user_true)
return user_true

while True:
sk = socket.socket ( )
sk.connect ( ('127.0.0.1', 9005) )
if login()['result']:
print('登陆成功')
name = input('download/upload:')
sk.send(name.encode('utf-8'))
if name == 'upload':
file_abs = input('upload_flies:')
upload(file_abs)

elif name == 'download':
download()

elif name != 'upload' or name != 'download':
print('输入错误,请重新输入!')

sk.close()

猜你喜欢

转载自www.cnblogs.com/guyeam/p/11785342.html