研究这玩意有啥用?
1、用python做自动化运维,客户端发送收集好的信息(模拟post发送)
2、前后端分离,django只提供数据,前端用vuejs,nodejs实现
3、3端 立体通信
上代码:
url.py
- from django.conf.urls import url
- from django.contrib import admin
- from return_api import views
- urlpatterns = [
- url(r'^api', views.apis),
- ]
- from django.shortcuts import render
- from django.http import JsonResponse
- # Create your views here.
- def apis(request):
- data={'name':'你爹'} #返<span style="color:#ff0000;">回给客户端的数据</span>
- if request.method=="POST":
- print(request.POST) #<span style="color:#ff0000;">查看客户端发来的请求内容</span>
- return JsonResponse(data) #<span style="color:#ff0000;">通过 django内置的Json格式 丢给客户端数据</span>
client.py
- import requests
- url = 'http://127.0.0.1:8001/api' #django api路径
- parms = {
- 'name' : '客户端', #<span style="color:#ff0000;">发送给服务器的内容</span>
- }
- headers = { #请求头 是<span style="color:#ff0000;">浏览器正常的</span>就行 就这里弄了一天 - -!
- 'User-agent' : 'none/ofyourbusiness',
- 'Spam' : 'Eggs'
- }
- resp = requests.post(url, data=parms, headers=headers)#<span style="color:#ff0000;">发送请求</span>
- # Decoded text returned by the request
- text = resp.text
- print(json.loads(text))
服务器端
客户端: