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

django API 中接口的互相調(diào)用實例

 更新時間:2020年04月01日 14:34:21   作者:人生海海may  
這篇文章主要介紹了django API 中接口的互相調(diào)用實例,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧

我就廢話不多說了,還是直接上代碼吧!

 url = "http://%s:%s/api-token-auth/" % (ip, port)
 query_args = {
  "username": username,
  "password": password
 }
 resp = requests.post(url=url, data=query_args)
 token = json.loads(resp.text)["token"]
 headers = {"Authorization": "JWT" + " " + token}  # 拿到token,拼成headers


 post_url = "http://%s:%s/message/message-level-two/"% (ip, port)
 data = {
  "app": app,
  "url": url,
  "message_id": message_id,
  "head": head,
  "title": title,
  "userprofile_id_list": userprofile_id_list
 }
 headers = self.headers
 requests.post(url=post_url, data=data, headers=headers)

獲取當(dāng)前請求的ip和端口

 host_ip, host_port = self.request.META.get("HTTP_HOST").split(':')[0], \
        self.request.META.get("HTTP_HOST").split(':')[1]

常見的請求頭如下:

CONTENT_LENGTH – The length of the request body (as a string).
CONTENT_TYPE – The MIME type of the request body.
HTTP_ACCEPT – Acceptable content types for the response.
HTTP_ACCEPT_ENCODING – Acceptable encodings for the response.
HTTP_ACCEPT_LANGUAGE – Acceptable languages for the response.
HTTP_HOST – The HTTP Host header sent by the client.
HTTP_REFERER – The referring page, if any.
HTTP_USER_AGENT – The client's user-agent string.
QUERY_STRING – The query string, as a single (unparsed) string.
REMOTE_ADDR – The IP address of the client.
REMOTE_HOST – The hostname of the client.
REMOTE_USER – The user authenticated by the Web server, if any.
REQUEST_METHOD – A string such as "GET" or "POST".
SERVER_NAME – The hostname of the server.
SERVER_PORT – The port of the server (as a string).

獲取請求頭內(nèi)容的用META

示例:

def index(request):
 ip = request.META.get("REMOTE_ADDR")
 return HttpResponse("你的ip地址是%s"%ip)

http://10.254.30.27/1
self.kwargs[‘pk'] # 可以拿到后邊的 1

補充知識:django 使用requests請求相關(guān)接口

1、如果是get請求接口,并且需要帶相關(guān)參數(shù)的話,可以借鑒下面的代碼:

import requests
 
from django.http import JsonResponse
 
def get_info(request):
 url = 'http://www.baidu.com'
 params = {'id': 1, 'user': 'lin'}
 response = requests.get(url=url, params=params)
 return JsonResponse(response.text, safe=False)

這樣將會返回一串json的字符串?dāng)?shù)據(jù)。

2、如果是post請求接口,并且需要帶相關(guān)參數(shù)的話,可以借鑒下面的代碼:

import requests
 
from json import dumps
from django.http import JsonResponse
 
def get_info(request):
 url = 'http://www.baidu.com'
 data = {'id': 1, 'user': 'lin'}
 response = requests.post(url=url, data=dumps(data))
 return JsonResponse(response.text, safe=False)

注:

(1)、其中必須注意的為data這個參數(shù),必須要用dumps(data)轉(zhuǎn)換一下,不然會報錯,response狀態(tài)碼為400,bad request error 400 while using python requests.post function。

(2)、如果需要在post請求底下加相關(guān)請求頭的話,可以借鑒下面的代碼:

import requests
 
from json import dumps
from django.http import JsonResponse
 
def get_info(request):
 url = 'http://www.baidu.com'
 data = {'id': 1, 'user': 'lin'}
 headers = {'content-Type': 'application/json', 'Accept': '*/*'}
 response = requests.post(url=url, data=dumps(data), headers=headers)
 return JsonResponse(response.text, safe=False)

這里如果response的狀態(tài)碼報415錯誤的話,即HTTP請求415錯誤 – 不支持的媒體類型(Unsupported media type),這就是content-Type可能寫錯了,就要注意一下了,因為通常接口會封裝一些參數(shù)到請求頭底下。

以上這篇django API 中接口的互相調(diào)用實例就是小編分享給大家的全部內(nèi)容了,希望能給大家一個參考,也希望大家多多支持腳本之家。

相關(guān)文章

  • 詳解Python IO編程

    詳解Python IO編程

    這篇文章主要介紹了Python IO編程的相關(guān)資料,文中講解非常細致,代碼幫助大家更好的理解和學(xué)習(xí),感興趣的朋友可以了解下
    2020-07-07
  • python清洗疫情歷史數(shù)據(jù)的過程詳解

    python清洗疫情歷史數(shù)據(jù)的過程詳解

    這篇文章主要介紹了python清洗疫情歷史數(shù)據(jù),包括數(shù)據(jù)獲取方法及使用python讀取csv的詳細代碼,本文通過實例代碼給大家介紹的非常詳細,對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2022-05-05
  • Python字符串特性及常用字符串方法的簡單筆記

    Python字符串特性及常用字符串方法的簡單筆記

    這篇文章主要介紹了Python字符串特性及常見字符串方法的簡單筆記,為一些Pyhton入門學(xué)習(xí)中的基礎(chǔ)知識,需要的朋友可以參考下
    2016-01-01
  • Python3.8中使用f-strings調(diào)試

    Python3.8中使用f-strings調(diào)試

    這篇文章主要介紹了Python3.8中使用f-strings調(diào)試的相關(guān)知識,本文通過實例代碼給大家介紹的非常詳細,具有一定的參考借鑒價值,需要的朋友可以參考下
    2019-05-05
  • python+Django實現(xiàn)防止SQL注入的辦法

    python+Django實現(xiàn)防止SQL注入的辦法

    這篇文章主要介紹了python+Django實現(xiàn)防止SQL注入的辦法,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2019-10-10
  • 分享6個隱藏的python功能

    分享6個隱藏的python功能

    給大家詳細分析了6個隱藏的python功能,并詳細講解了每個功能用法,需要的朋友學(xué)習(xí)下吧。
    2017-12-12
  • Python對圖像進行灰度處理的代碼介紹

    Python對圖像進行灰度處理的代碼介紹

    這篇文章主要給大家介紹了關(guān)于Python對圖像進行灰度處理的相關(guān)資料,圖像灰度化是將一幅彩色圖像轉(zhuǎn)換為灰度化圖像的過程,文中通過代碼介紹的非常詳細,需要的朋友可以參考下
    2023-11-11
  • python命令行參數(shù)sys.argv使用示例

    python命令行參數(shù)sys.argv使用示例

    這篇文章主要介紹了python命令行參數(shù)sys.argv使用示例,大家參考使用吧
    2014-01-01
  • Python基于Pytorch的特征圖提取實例

    Python基于Pytorch的特征圖提取實例

    在利用深度學(xué)習(xí)進行分類時,有時需要對中間的特征圖進行提取操作,下面這篇文章主要給大家介紹了關(guān)于Python基于Pytorch的特征圖提取的相關(guān)資料,需要的朋友可以參考下
    2022-03-03
  • python print 格式化輸出,動態(tài)指定長度的實現(xiàn)

    python print 格式化輸出,動態(tài)指定長度的實現(xiàn)

    這篇文章主要介紹了python print 格式化輸出,動態(tài)指定長度的實現(xiàn),具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2020-04-04

最新評論