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

通过代理地址发送Text-Plain格式的请求报文到远程服务器,并获取请求响应报文。该请求消息头中"Content-Type"字段为"text/plain; charset=UTF-8"。用户可以根据需要新增请求头字段,只需传入请求头中待新增的除了'Content-Type'字段外的其他字段数据。注意:对于发送的请求数据里面存在中文的情况,请优先进行特殊处理。

    def client_post_text_plain_data_proxy_requests(self,request_url,textpalindata,headerdict={},**kwargs):
        #功能说明:通过代理地址以text/plain形式发送请求报文到指定的地址并获取请求响应报文
        #输入参数说明:接收请求的URL、请求报文数据、待添加到请求头中的字段值组成的字典,默认为{},表示不新增字段、http或者https请求代理地址组成的字典,格式例如为{'http': 'http://192.168.111.222:8888', 'https': 'http://192.168.333.444:8888'}。
        #输出参数:请求响应报文
        #by xiaocc[20180709]        

        #默认请求头
        head={"Content-Type":"text/plain; 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报文数据为(客户端 --> 服务器):
',textpalindata
        
        proxyurl= kwargs['proxyurl']
        print u'请求代理地址为:',proxyurl        
        #发送请求报文到服务端
        r = requests.post(request_url,data=textpalindata,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

  

原文地址:https://www.cnblogs.com/apple2016/p/14250229.html