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)
相关推荐
夜雪一千2 分钟前
应用层加密的优缺点
python
计算机编程指导师13 分钟前
【计算机毕设选题推荐】基于Hadoop+Spark的化妆品销售大数据分析系统源码 毕业设计 选题推荐 毕设选题 数据分析 机器学习
大数据·hadoop·python·spark·毕业设计·课程设计·化妆品
用户03321266636726 分钟前
Python 实用技巧:从零自主生成结构化 PDF 页面
python
benchmark_cc27 分钟前
前复权数据每天可能变化,历史数据库应该覆盖还是保留原始价格?
大数据·python·pandas·量化交易·股票数据·quantdash
计算机编程指导师41 分钟前
计算机毕设选题推荐:Hadoop大数据下的个体肥胖健康风险评估与可视化系统源码 毕业设计 毕设选题 数据分析 机器学习
大数据·hadoop·python·spark·毕业设计·课程设计·肥胖健康风险
code2cat44 分钟前
【随笔】MCP输入Schema:把参数错误拦在工具执行之前
python·json schema·ai agent·mcp
计算机编程指导师1 小时前
Python大数据毕设怎么写?Django结合Hadoop+Spark京东销售分析系统源码 毕业设计 选题推荐 毕设选题 数据分析 机器学习
大数据·hadoop·python·spark·毕业设计·课程设计·京东商品
合尘猫1 小时前
AI 项目上线就停:一次事件循环阻塞的完整排查过程
人工智能·后端·python
染指11101 小时前
142.Agent-多Agent框架-通过Skills实现多Agent
开发语言·人工智能·python·langchain·agents
天国梦1 小时前
国密门禁品牌怎么选?2026年最新3个避坑要点
运维·服务器·数据库·python