使用python上传下载文件
使用python上传下载文件
前面我们使用nginx配置了文件上传下载服务器,本文我们学习使用python3上传下载相关文件。
1. 下载文件
# python3, 下载文件并保存
import urllib.request
import os
def download_file(filename):
# url为拼接出的真实下载地址
url = f"http://127.0.0.1:88/{filename}"
# headers为认证信息, YWRtaW46YWRtaW4= 为user:password的base64编码
headers = {
'Authorization': 'Basic YWRtaW46YWRtaW4=',
}
# 以下代码为获取本地文件大小与下载文件大小,如果相同则返回成功,否则继续下载(断点续传)
request =urllib.request.Request(url, headers=headers)
response = urllib.request.urlopen(request)
total_size = response.length
local_size = 0
if os.path.exists(filename):
local_size = os.path.getsize(filename)
if total_size == local_size:
return
headers = {
'Authorization': 'Basic YWRtaW46YWRtaW4=',
'Range': 'bytes=%d-' % (local_size),
}
request =urllib.request.Request(url, headers=headers)
response = urllib.request.urlopen(request)
CHUNK = 128 * 1024
with open(filename,"ab") as f:
while True:
chunk = response.read(CHUNK)
if not chunk:
break
f.write(chunk)
2. 文件sha1校验
对于从服务器上下载大文件,一般需要进行md5/sha1校验,我们假定服务器上已存在下载文件的sha1值,校验值的文件名为原文件名+后缀.sha1
import urllib.request
import hashlib
def check_file_sha1(filename):
# url为拼接出sha1的真实下载地址
url = f"http://127.0.0.1/{filename}.sha1"
# headers为认证信息
headers = {
'Authorization': 'Basic YWRtaW46YWRtaW4=',
}
request =urllib.request.Request(url, headers=headers)
response = urllib.request.urlopen(request)
origin_sha1 = str(response.read(), 'utf-8')
# 计算本地文件的sha1值并进行对比
myhash = hashlib.sha1()
CHUNK = 128 * 1024
with open(filename,"rb") as f:
while True:
chunk = f.read(CHUNK)
if not chunk:
break
myhash.update(chunk)
now_sha1 = myhash.hexdigest()
if origin_sha1 == now_sha1:
return True
else:
return False
3. 上传文件
# filepath 需要上传的文件的路径,如果仅包含文件名,则默认为当前路径
import requests
def upload(filepath):
# url为拼接出的真实上传地址
url = f"http://127.0.0.1/upload"
# headers为认证信息
headers = {
'Authorization': 'Basic YWRtaW46YWRtaW4=',
}
files = {
"file": open(filepath, 'rb')
}
response = requests.post(url, files=files, headers=headers)
print (response.text)
参考资料
- Python3 urllib.request库的基本使用, https://blog.csdn.net/kkfd1002/article/details/80306398
- python3 Requests常用操作, https://www.jianshu.com/p/c6b6aafe72bd
- python3 的md5和sha1加密, https://blog.csdn.net/xiaozhiit/article/details/82794003