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)
相关推荐
梦想不只是梦与想3 分钟前
Python 中的装饰器
python·装饰器
我叫唧唧波31 分钟前
Python+AI 全栈学习笔记
人工智能·python·学习
copyer_xyf1 小时前
Python 异常处理
前端·后端·python
麻雀飞吧2 小时前
期货多合约策略目标持仓怎么更新才不乱
python·区块链
Cthy_hy2 小时前
拓扑排序超详解:原理 + Kahn 贪心算法
python·算法·贪心算法
LSssT.2 小时前
【01】Python 机器学习
开发语言·python
为爱停留2 小时前
给智能体装上「刹车」:中断(Interrupts)与人工审批全解析
python
l1t2 小时前
DeepSeek总结的使用实体-组件-系统和基于存在性处理进行Python编程39-40
开发语言·python
曾阿伦3 小时前
Python 搭建简易HTTP服务
开发语言·python·http
MIUMIUKK3 小时前
从语法层面,看懂 Python 的特殊处
java·开发语言·python