熱線電話:13121318867

登錄
首頁精彩閱讀Python中用post、get方式提交數據的方法示例
Python中用post、get方式提交數據的方法示例
2018-06-19
收藏

Python中用post、get方式提交數據的方法示例

最近在使用Python的過程中,發現網上很少提到在使用post方式時,怎么傳一個數組作為參數的示例,此處根據自己的實踐經驗,給出相關示例,下面話不多說了,來一起跟著小編學習學習吧。
示例如下:
單純的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格式的參數
 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
總結
以上就是這篇文章的全部內容了,希望本文的內容對大家的學習或者工作能帶來一定的幫助

數據分析咨詢請掃描二維碼

若不方便掃碼,搜微信號:CDAshujufenxi

數據分析師資訊
更多

OK
客服在線
立即咨詢
日韩人妻系列无码专区视频,先锋高清无码,无码免费视欧非,国精产品一区一区三区无码
客服在線
立即咨詢