【原创】Python 发送form-data请求

发布于 2019-11-11  124 次阅读


# multipart/form-data
class MultipartFormData(object):
    """multipart/form-data格式转化"""

    @staticmethod
    def format(data, boundary="----WebKitFormBoundary7MA4YWxkTrZu0gW", headers={}):
        """
        form data
        :param: data:  {"req":{"cno":"18990876","flag":"Y"},"ts":1,"sig":1,"v": 2.0}
        :param: boundary: "----WebKitFormBoundary7MA4YWxkTrZu0gW"
        :param: headers: 包含boundary的头信息;如果boundary与headers同时存在以headers为准
        :return: str
        :rtype: str
        """
        # 从headers中提取boundary信息
        if "content-type" in headers:
            fd_val = str(headers["content-type"])
            if "boundary" in fd_val:
                fd_val = fd_val.split(";")[1].strip()
                boundary = fd_val.split("=")[1].strip()
            else:
                raise 22
                # raise "multipart/form-data头信息错误,请检查content-type key是否包含boundary"
        # form-data格式定式
        jion_str = '--{}\r\nContent-Disposition: form-data; name="{}"\r\n\r\n{}\r\n'
        end_str = "--{}--".format(boundary)
        args_str = ""

        if not isinstance(data, dict):
            raise 11
            # raise "multipart/form-data参数错误,data参数应为dict类型"
        for key, value in data.items():
            args_str = args_str + jion_str.format(boundary, key, value)

        args_str = args_str + end_str.format(boundary)
        args_str = args_str.replace("\'", "\"")
        return args_str

    def tc_res(self,tc_url,tc_name,tc_args):
        '''
        返回post form-data后的结果
        :param tc_url: 请求的url
        :param tc_name: 可配置项
        :param tc_args: 传递的参数
        :return: 
        '''
        headers = {
            'content-type': "multipart/form-data; boundary=----WebKitFormBoundary7MA4YWxkTrZu0gW",
            'cache-control': "no-cache",
        }
        data = {
            "name": tc_name,
            "args": tc_args
        }
        formdata = self.format(data=data,headers=headers)
        res = requests.post(url=tc_url,data=formdata, headers=headers)
        return res

tc_res 是我个人调用的封装,可以根据自己的需求修改