新聞中心
這里有您想知道的互聯(lián)網(wǎng)營(yíng)銷解決方案
python基于FTP實(shí)現(xiàn)文件傳輸相關(guān)功能代碼實(shí)例-創(chuàng)新互聯(lián)
這篇文章主要介紹了python基于FTP實(shí)現(xiàn)文件傳輸相關(guān)功能代碼實(shí)例,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
本實(shí)例有文件傳輸相關(guān)功能,包括:文件校驗(yàn)、進(jìn)度條打印、斷點(diǎn)續(xù)傳
客戶端示例:
import socket import json import os import hashlib CODE = { '1001':'重新上傳文件' } def file_md5(file_path): obj = open(file_path,'rb') m = hashlib.md5() for line in obj: m.update(line) obj.close() return m.hexdigest() def jdt(size,total_size): ''' 顯示進(jìn)度條 ''' val = int(size/total_size * 100) print('\r%s%%|%s' %(val,'#'*val,),end='') def send_file(exist_size,file_total_size): ''' 發(fā)送文件 :param exist_size: 開始讀取字節(jié)的位置 :param file_total_size: 文件大小 :return: ''' f = open(file_path, 'rb') f.seek(exist_size) send_size = exist_size while send_size < file_total_size: data = f.read(1024) sk.sendall(data) send_size += len(data) jdt(send_size,file_total_size) f.close() print('上傳成功') def upload(file_path): '''文件上傳(含斷點(diǎn))''' file_path = '111.jpg' file_md5_val = file_md5(file_path) file_name = os.path.basename(file_path) file_size = os.stat(file_path).st_size cmd_dict = {'cmd': 'upload', 'file_name': file_name, 'size': file_size, 'md5': file_md5_val} upload_cmd_bytes = json.dumps(cmd_dict).encode('utf8') sk.sendall(upload_cmd_bytes) # 2.等待服務(wù)端相應(yīng) response = json.loads(sk.recv(8096).decode('utf8')) if response['code'] == 1001: send_file(0,file_size) else: # 斷點(diǎn)續(xù)傳 exist_size = response('size') send_file(exist_size,file_size) sk = socket.socket() sk.connect(('127.0.0.1',8001)) while 1: #upload|文件路徑 user_input = input('命令>>>') # 1、自定義協(xié)議{'cmd':'upload','file_name':'...'} cmd,file_path = user_input.split('|',maxsplit=1) if cmd == 'upload': upload(file_path) elif cmd == 'download': pass
新聞標(biāo)題:python基于FTP實(shí)現(xiàn)文件傳輸相關(guān)功能代碼實(shí)例-創(chuàng)新互聯(lián)
網(wǎng)址分享:http://fisionsoft.com.cn/article/hhiid.html