欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Python使用post及get方式提交數(shù)據(jù)的實例

 更新時間:2019年01月24日 10:06:58   作者:youzhouliu  
今天小編就為大家分享一篇關于Python使用post及get方式提交數(shù)據(jù)的實例,小編覺得內容挺不錯的,現(xiàn)在分享給大家,具有很好的參考價值,需要的朋友一起跟隨小編來看看吧

最近在使用Python的過程中,發(fā)現(xiàn)網(wǎng)上很少提到在使用post方式時,怎么傳一個數(shù)組作為參數(shù)的示例,此處根據(jù)自己的實踐經(jīng)驗,給出相關示例:

單純的post請求:

def http_post():
  url = "http://152.1.12.11:8080/web"
  postdata = dict(d=2, p=10)
  post = []
  post.append(postdata)
  req = urllib2.Request(url, json.dumps(post)) #需要是json格式的參數(shù)
  req.add_header('Content-Type', 'application/json') #要非常注意這行代碼的寫法
  response = urllib2.urlopen(req)
  result = json.loads(response.read())
  print result

需要token時寫法如下:

def http_post():
  url = "http://152.1.12.11:8080/web"
  postdata = dict(d=2, p=10)
  post = []
  post.append(postdata)
  req = urllib2.Request(url, json.dumps(post))
  access_token = 'eyJ0eXAiOiJKV1QiLCJhbGciOiJSUzI1NiJ9.eyJ1bmlxdWVfbmFtZSI6I..........'
  req.add_header('Authorization', access_token) #header中添加token
  req.add_header('Content-Type', 'application/json') #要非常注意這行代碼的寫法
  response = urllib2.urlopen(req)
  result = json.loads(response.read())
  print result

get方式的寫法如下:

def get_access_token():
  local_url = 'http://152.1.1.1:8080/web'
  response = urllib2.urlopen(local_url).read()
  resp = json.loads(response)
  print resp

總結

以上就是這篇文章的全部內容了,希望本文的內容對大家的學習或者工作具有一定的參考學習價值,謝謝大家對腳本之家的支持。如果你想了解更多相關內容請查看下面相關鏈接

相關文章

最新評論