• python使用requests通过代理地址发送text/xml报文数据


     通过代理地址发送xml格式的请求报文到远程服务器,并获取请求响应报文。该请求消息头中"Content-Type"字段为"text/xml; charset=UTF-8"。用户可以根据需要新增请求头字段,只需传入请求头中待新增的除了'Content-Type'字段外的其他字段数据。

        def client_post_xmldata_proxy_requests(self,request_url,requestxmldata,headerdict={},**kwargs):
            #功能说明:通过代理地址发送text/xml请求报文到指定的地址并获取请求响应报文
            #输入参数说明:接收请求的URL,xml请求报文数据,待添加到请求头中的字段值组成的字典,默认为{},表示不新增字段、http或者https请求代理地址组成的字典,格式例如为{'http': 'http://192.168.111.222:8888', 'https': 'http://192.168.333.444:8888'}。
            #输出参数:请求响应报文
            #by xiaocc[20180709]        
    
            #默认请求头
            head={"Content-Type":"text/xml; charset=UTF-8", 'Connection': 'close'}
            print u'请求头待更新的数据:',headerdict,u'该数据类型为:',type(headerdict)
            #比对输入的header与默认的head,根据输入要求更新请求头
            if headerdict=='{}':#若未传入更新的请求头数据,则选择默认请求头
                pass
            elif type(headerdict) in (dict,): #若传入的更新的请求头数据为字典格式,则更新到默认请求头
                for key in headerdict: #判断待添加到默认head中的键值对
                    head[key]=headerdict[key] #根据输入更新默认head数据
            else:
                logging.error(u'请确保输入的请求头更新数据格式为字典格式!' )
                raise Exception
            print '服务器接收请求报文的地址为:',request_url
            print '客户端请求xml报文数据为(客户端 --> 服务器):
    ',requestxmldata
            
            proxyurl= kwargs['proxyurl']
            print u'请求代理地址为:',proxyurl        
            #发送请求报文到服务端
            r = requests.post(request_url,data=requestxmldata,headers=head,proxies=proxyurl,verify=False)
            print '请求头headers为: ',r.request.headers
            
            #获取请求响应报文数据
            responsedata=r.text
            print "get the status: ",r.status_code
            print '服务器响应报文为(客户端  <-- 服务器): ',responsedata
              
            #返回请求响应报文
            return responsedata
    

      

  • 相关阅读:
    Reactor模式
    libcurl安装
    libcurl
    http概述
    添物不花钱学JavaEE(基础篇) --HTML
    Android BGABadgeView:BGABadgeFrameLayout(5)
    添物不花钱学计算机及编程(预备篇)— 总述
    Android BGABadgeView:BGABadgeImageView以及BGABadgeRelativeLayout(4)
    Android RoundedBitmapDrawable:Android官方的圆角图形图象实现方案
    Android BGABadgeView:BGABadgeLinearLayout以整体线性布局作为BadgeView(3)
  • 原文地址:https://www.cnblogs.com/apple2016/p/14250278.html
Copyright © 2020-2023  润新知