使用pycURL發送請求

pycURl是python編寫的ibcurl.的接口,今天在使用的時候,出現了一個小問題,我發送的數據對方沒法正常接收,後來才知道是content-type設置不正確的緣由.python

pyCURL默認的Content-Type是application/x-www-form-urlencoded,也就是說,若是沒有明確設定,則發送的數據是url編碼以後的數據.json

但是若是用這個工具發送json字符串 的request body,則必須顯式指定json對應的內容類型Content-Type:application/json;charset=xxxx,不然接收方收到的數據是錯誤的.cookie

如下是我寫的一個簡易curl,具備基本的獲取響應數據,和上傳文件,攜帶cookie,保存cookie等功能.app

 
 
import os
import pycurl
import urllib
from StringIO import StringIO

'''
url:請求的url cookies:攜帶的cookie posdata:請求體,能夠是字符串,或者其餘能夠urlencode的類型 uploadfiles:上傳文件的參數,格式爲{'name':filename,'path':filepath} referer:來源 httpheader:自定義header頭 ua:客戶端代理名 cookiejar:存儲請求響應後的cookie的文件路徑 customrequest:自定義請求方法 注意: 1.暫時沒有進行ssl驗證 2.因爲後續須要對請求獲取各類數據,好比HTTP_CODE,因此請求發出後並無關閉,而是將pycurl.Curl對象返回方便查詢 ''' def myCurl(url,cookies='',postdata='',uploadfiles='',referer='',httpheader='',ua='',cookiejar='',customrequest=''): buffer = StringIO() c = pycurl.Curl() c.setopt(c.WRITEDATA, buffer) c.setopt(pycurl.SSL_VERIFYPEER, 0) c.setopt(pycurl.SSL_VERIFYPEER, 0) c.setopt(pycurl.SSL_VERIFYHOST, 0) c.setopt(pycurl.FOLLOWLOCATION, 1) if(ua == ''): c.setopt(pycurl.USERAGENT,'Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/61.0.3163.79 Safari/537.36') else: c.setopt(pycurl.USERAGENT,ua) if(httpheader): c.setopt(pycurl.HTTPHEADER,httpheader) if(referer): c.setopt(pycurl.REFERER, referer) if(cookies): c.setopt(pycurl.COOKIE,cookies) c.setopt(pycurl.URL,url) if(customrequest): c.setopt(pycurl.CUSTOMREQUEST, customrequest) if(postdata): if isinstance(postdata,str):#字符串不要使用urlencode postfields=postdata else: postfields=urllib.urlencode(postdata) c.setopt(c.POSTFIELDS, postfields) if(uploadfiles):#若是須要上傳文件 multipartFormData=[] fileTuple=(uploadfiles['name'], ( # upload the contents of this file c.FORM_FILE, uploadfiles['path'], )) multipartFormData.append(fileTuple) if (postdata): #除了文件還要上傳其餘一些數據,也要加上 for index in postdata: if(isinstance(postdata[index],int)): postdata[index]=str(postdata[index]) multipartFormData.append((index,postdata[index])) c.setopt(c.HTTPPOST, multipartFormData) if cookiejar: c.setopt(pycurl.COOKIEJAR, cookiejar) res=c.perform() if cookiejar: cookieStr = '' for line in open(cookiejar): if (line.find('#') == 0 or line == '\n'): # 注意換行並非空字符串,也要去掉 continue line = line.strip('\n') # 去掉空字符串 lineArr = line.split('\t') # 根據製表符切開 length = len(lineArr) name = lineArr[length - 2] value = lineArr[length - 1] cookieStr = cookieStr + name + '=' + value + '; ' cookieStr.strip('; ') os.remove(cookiejar) if cookiejar: data = {'c': c, 'data': buffer.getvalue(),'cookies':cookieStr} else: data = {'c': c, 'data': buffer.getvalue()} #c.close() # getinfo must be called before close. return data
相關文章
相關標籤/搜索