溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

python3發(fā)送post請(qǐng)求參數(shù)為空怎么辦

發(fā)布時(shí)間:2021-07-26 14:36:38 來(lái)源:億速云 閱讀:280 作者:小新 欄目:開發(fā)技術(shù)

小編給大家分享一下python3發(fā)送post請(qǐng)求參數(shù)為空怎么辦,相信大部分人都還不怎么了解,因此分享這篇文章給大家參考一下,希望大家閱讀完這篇文章后大有收獲,下面讓我們一起去了解一下吧!

post請(qǐng)求的時(shí)候如果不帶參數(shù),其實(shí)作用就跟get請(qǐng)求一樣。我們?cè)谧鼋涌跍y(cè)試的時(shí)候,發(fā)現(xiàn)開發(fā)就全部使用的post,get的作用就被這樣的post空參數(shù)請(qǐng)求給替代了。

在Python代碼請(qǐng)求,如下:

class HttpHelper():
 
 def __init__(self):
  '''獲取driver對(duì)象,和接口ip地址信息,里面的方法大家可以忽略,根據(jù)自己的情況來(lái)設(shè)置
  '''
  self.dr=Common.driver
  run_info=Common().get_current_run_config()
  app_info=Common().get_app_config()[run_info['_envir']]
  self.ip=app_info['url'].split('/')[2]
 
 def post(self,module,interface_name,post_para={}):
  '''arg: module 模塊名
    interface_name 接口名稱
    post_para  請(qǐng)求參數(shù),默認(rèn)是空字典,如果不填這個(gè)參數(shù)就是post請(qǐng)求參數(shù)為空的情況
  '''
  inter_info=Common().get_interface_info()[module]
  url='http://'+self.ip+inter_info[interface_name]['url']
  Common().logger_info("request - api - "+url)
  
  postdata = bytes(urllib.parse.urlencode(post_para), encoding='utf8') 
  Common().logger_info("request - arg - "+str(post_para))
  _jid=Common().get_jsessionid(self.dr) #獲取sessionid,這個(gè)方法是通過(guò)selenium的get_cookie方法來(lái)獲取sessionid,大家可以參考我其他的文章
  header={
   'Accept':'application/json, text/plain, */*',
   'Connection': 'keep-alive',
   'Content-Type':'application/x-www-form-urlencoded',
   'Cookie':'JSESSIONID='+_jid+'',
   'Host': ''+self.ip+'',
   'Origin': 'http://'+self.ip+''
   }
  Common().logger_info("[header] - "+str(header))
  try:
   req=urllib.request.Request(url,postdata,header)
   with urllib.request.urlopen(req) as resp:
    response=resp.read().decode('utf-8')
    response=json.loads(response)
    Common().logger_info('response - '+str(response))
    if response['data']!='':
     Common().logger_info('http post success!!!')
    return response
  except Exception as e:
   Common().logger_error(str(e))

以上是“python3發(fā)送post請(qǐng)求參數(shù)為空怎么辦”這篇文章的所有內(nèi)容,感謝各位的閱讀!相信大家都有了一定的了解,希望分享的內(nèi)容對(duì)大家有所幫助,如果還想學(xué)習(xí)更多知識(shí),歡迎關(guān)注億速云行業(yè)資訊頻道!

向AI問(wèn)一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI