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)
相关推荐
u***32432 小时前
使用python进行PostgreSQL 数据库连接
数据库·python·postgresql
青瓷程序设计4 小时前
动物识别系统【最新版】Python+TensorFlow+Vue3+Django+人工智能+深度学习+卷积神经网络算法
人工智能·python·深度学习
tobebetter95275 小时前
How to manage python versions on windows
开发语言·windows·python
F_D_Z5 小时前
数据集相关类代码回顾理解 | sns.distplot\%matplotlib inline\sns.scatterplot
python·深度学习·matplotlib
daidaidaiyu6 小时前
一文入门 LangGraph 开发
python·ai
不知更鸟7 小时前
前端报错:快速解决Django接口404问题
前端·python·django
4***72137 小时前
【玩转全栈】----Django模板语法、请求与响应
数据库·python·django
梁正雄7 小时前
1、python基础语法
开发语言·python
ituff8 小时前
微软认证考试又免费了
后端·python·flask
梁正雄9 小时前
2、Python流程控制
开发语言·python