登录作业,完成上传下载

server.py:

import socket
import json,hashlib
import struct,os,sys

def my_send(conn,dic):
str_dic=json.dumps(dic)
b_dic=str_dic.encode('utf-8')
mlen=struct.pack('i',len(b_dic))
conn.send(mlen)
conn.send(b_dic)

def my_recv(conn):
mlen=conn.recv(4)
len_dic=struct.unpack('i',mlen)[0]
str_dic=conn.recv(len_dic).decode('utf-8')
dic=json.loads(str_dic)
return dic

def get_md5(name,password):
md5=hashlib.md5(name.encode('utf-8'))
md5.update(password.encode('utf-8'))
return md5.hexdigest()

def upload():
dic=my_recv(conn)
with open(dic['filename'],mode='wb') as f:
while dic['filesize']>0:
content=conn.recv(1024)
dic['filesize']-=len(content)
f.write(content)

def dowload():
abs_path=r"J:迅雷下载2HEYZO-2064 顔射でどろべちゃ!ぶっかけ祭り!!Vol.4 – メイリン.mp4"
filename=os.path.basename(abs_path)
filesize=os.path.getsize(abs_path)
dic={'filename':filename,'filesize':filesize}
my_send(conn,dic)
with open(abs_path,mode='rb') as f:
while filesize>0:
content=f.read(1024)
filesize-=len(content)
conn.send(content)

def login(conn):
flag=True
while flag:
msg=my_recv(conn)
with open(r'D:untitled9userinfo','r',encoding='utf-8') as f:
for line in f :
name,pwd=line.strip().split('|')
if name==msg['username'] and pwd==get_md5(name,msg['password']):
res,flag=True,False
break
else:
res=False
dic={'operate':'login','result':res}
my_send(conn,dic)

sk=socket.socket()
sk.bind(('127.0.0.1',8009))
sk.listen()
conn,_=sk.accept()

login(conn)
opt=conn.recv(1024).decode('utf-8') #接收用户选择。
if hasattr(sys.modules[__name__],opt):
getattr(sys.modules[__name__],opt)()

conn.close()
sk.close()

# 无论在客户端还是服务端,dowload和upload都不需要传参。
# my_recv()必须要有返回值。
# 必须发送、接收用户的选择数据。


client.py:
import socket
import os,sys
import json
import struct

def my_recv(sk):
mlen=sk.recv(4)
len_dic=struct.unpack('i',mlen)[0]
str_dic=sk.recv(len_dic).decode('utf-8')
dic=json.loads(str_dic)
return dic #注意:必须有返回值。

def my_send(sk,dic):
str_dic=json.dumps(dic)
b_dic=str_dic.encode('utf-8')
mlen=struct.pack('i',len(b_dic))
sk.send(mlen)
sk.send(b_dic)

def upload():
abs_path=r"D:BaiduYunDownload厉害了!教你用手机拍出格的照片(完结)第9期第9期文艺清新照片攻略.mp4"
filename=os.path.basename(abs_path)
filesize=os.path.getsize(abs_path)
dic={'filename':filename,'filesize':filesize}
my_send(sk,dic)
with open(abs_path,mode='rb') as f:
while filesize>0:
content=f.read(1024)
filesize-=len(content)
sk.send(content)

def dowload():
opt_dic={'operate':'dowload'}
my_send(sk,opt_dic)
msg=my_recv(sk)
with open(msg['filename'],mode='wb') as f:
while msg['filesize']>0:
content=sk.recv(1024)
msg['filesize']-=len(content)
f.write(content)

def login(sk):
while True:
usr=input('请输入用户名:').strip()
pwd=input(('请输入密码:')).strip()
dic={'username':usr,'password':pwd}
my_send(sk,dic)
dic=my_recv(sk)
if dic['operate']=='login' and dic['result']:
print('登陆成功!')
break
else:
print('登录失败,请检查后输入。')

sk=socket.socket()
sk.connect(('127.0.0.1',8009))

login(sk)

opt_lst=['upload','dowload']
for index,opt in enumerate(opt_lst,1):
print(index,opt)
num=int(input('请选择你要操作的序号:'))
sk.send(opt_lst[num-1].encode('utf-8'))#发送用户选择给服务器。
getattr(sys.modules[__name__],opt_lst[num-1])()

sk.close()

尚未完成发送文件目录,选择文件的功能。

原文地址:https://www.cnblogs.com/qqq789001/p/13675757.html