python使用requests發送text/plain報文數據


 

    def client_post_text_plain_data_requests(self,request_url,textpalindata,headerdict={}):
        #功能說明:以text/plain形式發送請求報文到指定的地址並獲取請求響應報文
        #輸入參數說明:接收請求的URL,請求報文數據,待添加到請求頭中的字段值組成的字典
        #輸出參數:請求響應報文
        #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報文數據為(客戶端  --> 服務器):\n',textpalindata
      
        #發送請求報文到商戶前置
        r = requests.post(request_url,data=textpalindata,headers=head)
        print '請求頭headers為: ',r.request.headers
        
        #獲取請求響應報文數據
        responsedata=r.text
        print "get the status: ",r.status_code
        print '服務器響應報文為(客戶端  <-- 服務器): ',responsedata
          
        #返回請求響應報文
        return responsedata

  


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM