成人在线亚洲_国产日韩视频一区二区三区_久久久国产精品_99国内精品久久久久久久

您的位置:首頁(yè)技術(shù)文章
文章詳情頁(yè)

python 如何調(diào)用遠(yuǎn)程接口

瀏覽:4日期:2022-07-11 14:56:13

在python中我們可以使用requests模塊來(lái)實(shí)現(xiàn)調(diào)用遠(yuǎn)程接口

一:安裝requests模塊

pip install requests

二:使用requests模塊實(shí)現(xiàn)get方式調(diào)用遠(yuǎn)程接口

使用get方式調(diào)用遠(yuǎn)程接口主要使用了requests模塊的get方法

requests.get()

get方法常見(jiàn)的參數(shù)有url,params和headers

url:表示遠(yuǎn)程接口的地址 params表示get參數(shù) headers表示get傳參的headers參數(shù)信息

使用requests模塊實(shí)現(xiàn)get方式調(diào)用遠(yuǎn)程接口的簡(jiǎn)單實(shí)現(xiàn)如下

# -*- coding: utf-8 -*-import requestsimport ast#接口地址url = ’XXX’#get傳參data = {’type’:’0’}#headers信息headers = { ’Content-Type’: ’application/x-www-form-urlencoded’, ’Authorization’: ’Bearer XXX’}#r = requests.get(url, params=data, headers = headers)# 接口返回的狀態(tài)碼print(r.status_code)# 接口返回的字符串內(nèi)容content = r.text# #將字符串轉(zhuǎn)字典型content_list = ast.literal_eval(content)print(content_list)# 接口返回的json格式內(nèi)容print(r.json())

根據(jù)如上就可以實(shí)現(xiàn)使用get方式調(diào)用遠(yuǎn)程接口

三:使用requests模塊實(shí)現(xiàn)post方式調(diào)用遠(yuǎn)程接口

使用post方式調(diào)用遠(yuǎn)程接口主要使用了requests模塊的post方法

requests.post()

post方法常見(jiàn)的參數(shù)有url,data和headers

url:表示遠(yuǎn)程接口的地址 data:表示post參數(shù) headers:表示post傳參的headers參數(shù)信息

使用requests模塊實(shí)現(xiàn)post方式調(diào)用遠(yuǎn)程接口的簡(jiǎn)單實(shí)現(xiàn)如下

# -*- coding: utf-8 -*-import requestsimport ast#接口地址url = ’XXX’#header信息headers = { ’Content-Type’: ’application/x-www-form-urlencoded’, ’Authorization’: ’Bearer XXX’}#post傳參data = { ’nickname’: ’111’, ’gender’: 1, ’city’: ’ce’, ’avatar’: ’111’}r = requests.post(url, data=data,headers=headers)# 接口返回的狀態(tài)碼print(r.status_code)# 接口返回的字符串內(nèi)容content = r.text# #將字符串轉(zhuǎn)字典型content_list = ast.literal_eval(content)print(content_list)# 接口返回的json格式內(nèi)容print(r.json())

以上就是python 如何調(diào)用遠(yuǎn)程接口的詳細(xì)內(nèi)容,更多關(guān)于python 調(diào)用遠(yuǎn)程接口的資料請(qǐng)關(guān)注好吧啦網(wǎng)其它相關(guān)文章!

標(biāo)簽: Python 編程
相關(guān)文章: