django API 中接口的互相调用实例


Posted in Python onApril 01, 2020

我就废话不多说了,还是直接上代码吧!

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)

获取当前请求的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).

获取请求头内容的用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请求相关接口

1、如果是get请求接口,并且需要带相关参数的话,可以借鉴下面的代码:

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的字符串数据。

2、如果是post请求接口,并且需要带相关参数的话,可以借鉴下面的代码:

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这个参数,必须要用dumps(data)转换一下,不然会报错,response状态码为400,bad request error 400 while using python requests.post function。

(2)、如果需要在post请求底下加相关请求头的话,可以借鉴下面的代码:

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的状态码报415错误的话,即HTTP请求415错误 ? 不支持的媒体类型(Unsupported media type),这就是content-Type可能写错了,就要注意一下了,因为通常接口会封装一些参数到请求头底下。

以上这篇django API 中接口的互相调用实例就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持三水点靠木。

Python 相关文章推荐
Python 实现简单的电话本功能
Aug 09 Python
Python生成随机验证码的两种方法
Dec 22 Python
Python实现的查询mysql数据库并通过邮件发送信息功能
May 17 Python
python人民币小写转大写辅助工具
Jun 20 Python
解读python如何实现决策树算法
Oct 11 Python
对pandas的算术运算和数据对齐实例详解
Dec 22 Python
python实现给微信指定好友定时发送消息
Apr 29 Python
python 自动轨迹绘制的实例代码
Jul 05 Python
Python+OpenCV实现旋转文本校正方式
Jan 09 Python
python如何判断IP地址合法性
Apr 05 Python
学会python自动收发邮件 代替你问候女友
May 20 Python
Python查找算法的实现 (线性、二分,分块、插值查找算法)
Apr 24 Python
完美解决pyinstaller打包报错找不到依赖pypiwin32或pywin32-ctypes的错误
Apr 01 #Python
Python greenlet和gevent使用代码示例解析
Apr 01 #Python
Django-rest-framework中过滤器的定制实例
Apr 01 #Python
Python如何操作office实现自动化及win32com.client的运用
Apr 01 #Python
Django之choices选项和富文本编辑器的使用详解
Apr 01 #Python
Python AutoCAD 系统设置的实现方法
Apr 01 #Python
django实现模型字段动态choice的操作
Apr 01 #Python
You might like
ThinkPHP模板比较标签用法详解
2014/06/30 PHP
CI框架(ajax分页,全选,反选,不选,批量删除)完整代码详解
2016/11/01 PHP
thinkPHP5.0框架API优化后的友好性分析
2017/03/17 PHP
phpcms配置列表页以及获得文章发布时间
2017/07/04 PHP
javascript Base类 包含基本的方法
2009/07/22 Javascript
用JS判别浏览器种类以及IE版本的几种方法小结
2011/08/02 Javascript
Javascript学习笔记之 对象篇(四) : for in 循环
2014/06/24 Javascript
jQuery简单几行代码实现tab切换
2015/03/10 Javascript
浅谈JavaScript正则表达式分组匹配
2015/04/10 Javascript
jQuery获取与设置iframe高度的方法
2016/08/01 Javascript
angularjs实现猜大小功能
2017/10/23 Javascript
深入理解Vue nextTick 机制
2018/04/28 Javascript
详解基于mpvue的小程序markdown适配解决方案
2018/05/08 Javascript
Vue中div contenteditable 的光标定位方法
2018/08/25 Javascript
js实现小星星游戏
2020/03/23 Javascript
js+canvas绘制图形验证码
2020/09/21 Javascript
Pyhthon中使用compileall模块编译源文件为pyc文件
2015/04/28 Python
Python 的类、继承和多态详解
2017/07/16 Python
python实现下载pop3邮件保存到本地
2018/06/19 Python
Python字符串逆序的实现方法【一题多解】
2019/02/18 Python
Python实现的文轩网爬虫完整示例
2019/05/16 Python
python使用itchat模块给心爱的人每天发天气预报
2019/11/25 Python
python实现在多维数组中挑选符合条件的全部元素
2019/11/26 Python
pymysql 插入数据 转义处理方式
2020/03/02 Python
浅析python字符串前加r、f、u、l 的区别
2021/01/24 Python
python实现杨辉三角的几种方法代码实例
2021/03/02 Python
联想英国官网:Lenovo英国
2019/07/17 全球购物
德国家用电器购物网站:Premiumshop24
2019/08/22 全球购物
Linux机考试题
2015/10/16 面试题
竞争上岗演讲稿
2014/01/05 职场文书
文明教师事迹材料
2014/01/16 职场文书
学校志愿者活动总结
2014/06/27 职场文书
法定代表人身份证明书(含说明)
2014/10/02 职场文书
店长岗位职责
2015/02/11 职场文书
吃通javascript正则表达式
2021/04/21 Javascript
MySQL中in和exists区别详解
2021/06/03 MySQL