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)
相关推荐
IAUTOMOBILE6 小时前
Python 流程控制与函数定义:从调试现场到工程实践
java·前端·python
TT_44198 小时前
python程序实现图片截图溯源功能
开发语言·python
小陈的进阶之路8 小时前
logging 日志模块笔记
python
cqbelt9 小时前
Python 并发编程实战学习笔记
笔记·python·学习
智算菩萨9 小时前
【论文复现】Applied Intelligence 2025:Auto-PU正例无标签学习的自动化实现与GPT-5.4辅助编程实战
论文阅读·python·gpt·学习·自动化·复现
小陈工10 小时前
2026年3月31日技术资讯洞察:AI智能体安全、异步编程突破与Python运行时演进
开发语言·jvm·数据库·人工智能·python·安全·oracle
老李的勺子10 小时前
Agent 记忆失效的 5 种方式:完整排查复盘
python·llm
Leo6553510 小时前
动态透视报表 + 查询接口 + Excel导出
开发语言·windows·python
清水白石00810 小时前
pytest Fixture 设计实战指南:作用域、依赖链、自动清理与测试资源高效复用
python·pytest
tottoramen11 小时前
如何安装龙虾
python