一、http
python
import requests
'''
通过http上传文件
:param file_path: 待上传文件的路径
:return: 返回的请求句柄
'''
def upload_files_by_http(file_path, url=http_url):
with open(file_path, 'rb') as f:
file_date = {'file': f}
url = url + 'submit'
r = requests.post(url=url, files=file_date)
return r
二、https
python
import requests
'''
通过https上传文件
:param file_path: 待上传文件的路径
:return: 返回的请求句柄
'''
def upload_files_by_https(file_path, url=http_url):
with open(file_path, 'rb') as f:
file_date = {'file': f}
url = url + 'submit'
r = requests.post(url=url, files=file_date, verify=False)
return r
三、封装使用
python
'''
此函数引用了上面的http传输方法,此函数是对结果加以校验
trans_res为传输文件结果验证方式,True为传输成功,False为传输拦截
'''
def http_upload_file(file_path, http_url, trans_res)
try:
rsp = upload_files_by_http(file_path, http_url)
result = rsp.json()
code = rsp.status_code
if trans_res:
assert result['result'] == 'successfully!' and result['status'] == 1 and code == 200
else:
assert result['result'] != 'successfully!' or result['status'] != 1 or code != 200
except Exception as err:
print(err)