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)
相关推荐
张二娃同学几秒前
第08篇_RNN_LSTM_GRU序列模型
人工智能·python·rnn·深度学习·神经网络·gru·lstm
财经资讯数据_灵砚智能1 分钟前
基于全球经济类多源新闻的NLP情感分析与数据可视化(夜间-次晨)2026年5月13日
大数据·人工智能·python·信息可视化·语言模型·自然语言处理
我鑫如一15 分钟前
专业的AI API中转站厂家
人工智能·python
如竟没有火炬17 分钟前
接雨水22
数据结构·python·算法·leetcode·散列表
消晨消晨17 分钟前
Pytorch初上手——Dataset自定义数据集与Dataloader数据加载器
人工智能·pytorch·python
小白学大数据31 分钟前
均线选股策略研究:基于 Python 数据分析实现
人工智能·python·数据分析
C137的本贾尼37 分钟前
从零认识 Spring AI:Java 开发者的 AI 第一课
python·langchain
源码之家38 分钟前
计算机毕业设计:Pyhon健康数据分析系统 Django框架 数据分析 可视化 身体数据分析 大数据(建议收藏)✅
大数据·python·数据挖掘·数据分析·django·lstm·课程设计
weixin_444012931 小时前
如何在MongoDB中实现按时间跨度的分片路由_时间序列范围分片与冷热节点架构
jvm·数据库·python
无敌昊哥战神1 小时前
大模型(LLM)推理优化技术全景总结
python·算法·大模型