python请求

    #1、xml格式化请求
    def xml_request(self):
        body = '''
                
                  
                    {0}
                    {1}
                    {2}
                    {3}
                    {4}
                  
                  {5}
                  {6}
                  {7}
                '''\
            .format(self.request_data['core_merchant_no'],
                    self.request_data['version'],
                    self.request_data['pay_channel'],
                    self.request_data['pay_transaction_id'],
                    self.request_data['out_trade_no'],
                    self.request_data['paymentId'],
                    self.request_data['reqSerialNum'],
                    self.request_data['memberId'],)
        #请求返回
        r = requests.post(self.url, data=body.encode("utf-8"))
        return r.text
    #2、json请求
    def json_request(self,zf_info):
        request={
            "body":
                {"out_trade_no":"{0}".format(zf_info['body']['out_trade_no'])},
            "head":
                {"service":"front.query",
                 "version":"1.0",
                 "partner_id":"10000002048131212",
                 "core_merchant_no":"{0}".format(zf_info['head']['core_merchant_no']),
                 "sign":"",
                 "sign_type":"MD5",
                 "input_charset":"UTF-8"} }
        params = {
            "requestJson": json.dumps(request)
        }
        r = requests.post(self.url, data=params)
        return r.text
   #3、文件上传请求
    def file_request(self):
        body={'reqXml':''
                       ''
                       ''
                       '{0}'
                       '{1}       '
                       '{2}'
                       '{3}'
                       ' {4}'
                       '     '
                       '{5}'
                       '{6}'
                       '{7}'
                       '{8}'
                       '{9}  '
                        .format(self.request_data['core_merchant_no'],
                                self.request_data['version'],
                                self.request_data['pay_channel'],
                                self.request_data['pay_transaction_id'],
                                self.request_data['out_trade_no'],
                                self.request_data['paymentId'],
                                self.request_data['reqSerialNum'],
                                self.request_data['memberId'],
                                self.request_data['photoType'],
                                self.request_data['picture'])}

        f={'picture':open('D:\\aa.jpg','rb')}
        #请求返回
        r = requests.post(self.url,data=body,files=f)
        return r.text
  

你可能感兴趣的:(python请求)