python——http/https文件传输

一、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)
相关推荐
人机(未转人工)41 分钟前
python 批量测试ip是否能ping( 多线程 )
python
cndes1 小时前
安装Miniconda+Jupyter notebook
ide·python·jupyter
Patrick在香港1 小时前
Python 拉取城巴开放数据:406 条路线,香港岛巴士网络的数字画像
网络·python·数据分析·php·数据可视化·香港·开放数据
for_ever_love__1 小时前
python基础语法学习: 正则表达式
python·学习·正则表达式
aiqianji1 小时前
功能全面的教AI短篇小说写作的软件都有哪些?
人工智能·python
轮到我狗叫了1 小时前
Slurm如何使用
人工智能·python·深度学习·机器学习
Ali885202 小时前
Python列表增删改查速查表和详细演示
python
新时代牛马2 小时前
softirq 与tasklet:从irq_exit 到__do_softirq 的下半部路径
python
tqs_123452 小时前
值传递与引用传递
java·开发语言·python
YsyaaabB2 小时前
Deep Agents 实战课程
python·langchain