Python教程

python requests_toolbelt模块传输csv文件

本文主要是介绍python requests_toolbelt模块传输csv文件,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

需求:测试导入相机信息功能,文件为csv格式

代码实现:

点击查看代码
import os
import requests
from urllib import parse
from requests_toolbelt import MultipartEncoder
from utils.handle_yaml import test_info
from utils.log_record import logger

env1 = test_info.env_info("env1")

file = os.path.join(os.path.dirname(os.path.dirname(os.path.dirname(__file__))), "test_data", "upload_cameraInfo.csv")


class TestUploadCameraInfo(object):
    """相机信息信息"""

    def test_upload_camera_info_succs(self, login):
        # 导入相机
        env1["headers"]["accessToken"] = login
        res_project = requests.get(parse.urljoin(env1["base_url"], "/api/project/query"),
                                   headers=env1["headers"]).json()
        site_id = res_project.get("data").get("siteId")

        url = parse.urljoin(env1["base_url"], "/api/camera/upload_camera_info/%s" % site_id)
        logger(url=url, method="POST")
        # 数据组装
        data = MultipartEncoder(fields={"file": ("upload_cameraInfo.csv", open(file, "rb"), "text/csv")})
        # 重写Content-Type
        env1["headers"]["Content-Type"] = data.content_type
        logger(headers=env1["headers"]["Content-Type"], data=data)

        res = requests.post(url, headers=env1["headers"], data=data).json()

        logger(response=str(res))

        exp_data = {
            "code": 0,
            "data": {},
            "message": "success"
        }

        assert res == exp_data

这篇关于python requests_toolbelt模块传输csv文件的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!