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

1 #server:

2

3 #!/usr/bin/env python

4 #-*- coding: utf-8 -*-

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

6

7 count = 'admin'

8 defget_md5(pwd):9 md5 = hashlib.md5(count.encode('utf-8'))10 md5.update(pwd.encode('utf-8'))11 ret =md5.hexdigest()12 returnret13

14 deffile_user():15 with open('password.log','r',encoding='utf-8') as f:    #password.log是存储用户名和密码的文件

16 for item inf:17 username,password = item.strip().split('|')18 dic = {'username':username,'password':get_md5(password)}19 if dic[ 'username' ] == user_name [ 'username' ] and dic[ 'password' ] == user_name ['password'] :20 return {'result': True}21 else:22 continue

23 return {'result': False}24

25 defreceive():26 byte_file_name = conn.recv(1024).decode('utf-8')27 file_name_1 =json.loads(byte_file_name)28

29 with open(os.path.join(fileabs_storage,file_name_1['filename']) ,'wb') as f:30 whileTrue:31 file_receive = conn.recv ( file_name_1 [ 'file_size'] )32 iffile_receive:33 f.write(file_receive)34 else:35 print ( '\033[32mupload--- \033[0m' + os.path.join(fileabs_storage,file_name_1['filename'] ))36 break

37

38 defsend_file():39 result =os.walk(fileabs_storage)40 file_list =[]41 for a,b,c inresult:42 for item inc:43 path =os.path.join(a,item)44 dire =os.path.dirname(path)45 file =os.path.basename(path)46 size =os.path.getsize(path)47 dic = {'file_dire':dire,'file_name':file,'file_size':size}48 file_list.append(dic)49 byte_list =json.dumps(file_list)50 conn.send(byte_list.encode('utf-8'))51 #print(path)

52 byte_file_name = conn.recv(1024).decode('utf-8')53 print('\033[32mdownload-- \033[0m'+byte_file_name)54 ifos.path.isabs(byte_file_name):55 conn.send ( 'True'.encode ( 'utf-8') )56 with open(byte_file_name,'rb') as f:57 whileTrue:58 name = f.read(20480000)59 ifname:60 conn.send(name)61 else:break

62 else:conn.send('False'.encode('utf-8'))63

64

65

66 sk =socket.socket()67 sk.bind(('127.0.0.1',9005))68 sk.listen()69 whileTrue:70 conn,addr =sk.accept()71

72

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

76 byte_user_true =json.dumps ( file_user ( ) )77 conn.send ( byte_user_true.encode ( 'utf-8') )78 if file_user()['result']:79 print('client已上线:')80 byte_load = conn.recv(1024).decode('utf-8')81 if byte_load == 'upload':82 receive()83 if byte_load == 'download':84 send_file()85

86 conn.close()87 sk.close()88

89

90 #client:

91

92 #!/usr/bin/env python

93 #-*- coding: utf-8 -*-

94 importos95 importsys96 importjson97 importsocket98 importhashlib99

100 user =False101 count = 'admin'

102 path_dir = 'D:\load_storage'

103

104 defget_md5(pwd):105 md5 = hashlib.md5(count.encode('utf-8'))106 md5.update(pwd.encode('utf-8'))107 ret =md5.hexdigest()108 returnret109

110 defupload(file):111 ifos.path.isabs(file):112 file_name =os.path.basename(file)113 file_size =os.path.getsize(file)114 file_dic = {'filename':file_name,'file_size':file_size}115 print('文件上传成功')116 byte_file =json.dumps(file_dic)117 sk.send(byte_file.encode('utf-8'))118 #方法一:

119 #with open(file,'rb') as f:

120 #seek_id = 0

121 #while True:

122 #f.seek(seek_id)

123 #admin = f.readline()

124 #if admin:

125 #sk.send(admin)

126 #seek_id = f.tell()

127 #else:break

128 #方法二:

129 with open ( file, 'rb') as f :130 whileTrue:131 user_name = f.read(20480000)132 ifuser_name:133 sk.send(user_name)134 else:return

135

136 defreceive_files(file_name,file_size,path):137 with open(os.path.join(path,file_name),'wb') as f:138 whileTrue:139 file_receive =sk.recv ( file_size )140 iffile_receive:141 f.write(file_receive)142 else:143 print('文件下载成功')144 sys.exit(0)145 '''

146 def file_whether_exists(path,file):147 download_file = os.path.join(path,file)148 if os.path.exists(download_file):149 return True150 return False151 '''

152 defdownload():153 file_name = sk.recv ( 1024 ).decode ( 'utf-8')154 file_name_dic =json.loads ( file_name )155 whileTrue:156 print ( '\033[31m可下载的文件:\033[0m')157 for item infile_name_dic :158 print ( '\033[32m%s\033[0m' % item['file_name'] )159

160 file_transport = input ( 'file_download:')161 #if not file_whether_exists(path_dir,file_transport):

162 for item infile_name_dic:163 if item['file_name'] ==file_transport:164 path = os.path.join(item['file_dire'],file_transport)165

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

169 if ret_true == 'True':170 print(item['file_size'])171 receive_files(file_transport,item['file_size'],path_dir)172 else:173 print('文件名输入错误,请重新输入')174 continue

175 #else:print('文件已存在,请重新选择')

176

177

178 deflogin():179 user = input ( 'username:')180 pwd = input ( 'password:')181 dic = {'username' : user, 'password': get_md5 ( pwd )}182 byte_dic =json.dumps ( dic )183 sk.send ( byte_dic.encode ( 'utf-8') )184 byte_user_true = sk.recv(1024).decode()185 user_true =json.loads(byte_user_true)186 returnuser_true187

188 whileTrue:189 sk =socket.socket ( )190 sk.connect ( ('127.0.0.1', 9005) )191 if login()['result']:192 print('登陆成功')193 user =True194 name = input('download/upload(n):')195 if name.upper() == 'N':196 break

197 sk.send(name.encode('utf-8'))198 if name == 'upload':199 file_abs = input('upload_flies:')200 upload(file_abs)201

202 elif name == 'download':203 download()204

205 elif name != 'upload' or name != 'download':206 print('输入错误,请重新输入!')207 else:208 print('登陆失败,请重新输入!')209 continue

210

211 sk.close()

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值