标签:sub encode har list shape 头信息 添加 input 映射
1 发送HTTP请求
postman构建HTTP请求:
1.1 requests模块
安装:pip install requests
导入模块:import requests
1.2 请求与响应
r = requests.方法(url,headers,data,......)
r = requests.get(‘https://github.com/timeline.json‘) r = requests.post("http://httpbin.org/post") r = requests.put("http://httpbin.org/put") r = requests.delete("http://httpbin.org/delete") r = requests.head("http://httpbin.org/get") r = requests.options("http://httpbin.org/get")
请求后获取常用的响应结果:
r.headers #获取返回的头信息 r.text #获取返回额主体 r.cookies #获取返回的cookie r.status_code #获取返回的状态码
1.2.1 请求参数
详情可参考:https://www.cnblogs.com/shapeL/p/9037035.html
常用post请求,请求正文是raw,可传入json格式文本:
import requests,json test_url=‘http://111.230.173.51:2018/app‘ h ={ ‘sid‘:‘1010‘, ‘partnerNo‘:‘15601‘, ‘Content-Type‘:‘application/json‘, } body ={ "serviceHeader":{ "sessionId":"", "userId":"", "deviceId":"990009263463478", "osType":"1", "version":"", "lang":"" }, "serviceBody":{} } response = requests.post(test_url,headers =h,data=json.dumps(body)) #另一种写法json=body print(response.status_code) print(response.headers) print(response.text)
2 发送HTTPS请求
url =‘https//www.zhihu.com‘ #只需要修改url response = requests.get(url,headers=h,cookie=c,params=p)
3 发送WebSocket请求
3.1 WebSocket模块
安装: pip install websocket
pip install websocket-client
导入模块: import websocket
3.2 请求与响应
3.2.1 建立连接,URL以ws开头
变量 = websocket.create_connection(url)
3.2.12 发送数据
变量.send(发送内容)
3.2.3 接收数据
变量.recv()
3.3 请求实例
import websocket url = ‘ws://www.xxx.com/xxxx‘ ws = websocket.create_connection(url) ws.send("{"request":1111,“service”:1001,"name":"xxxx"}") new.msg1 = ws.recv() print (new.msg1) ws.send("{"request":1111,“service”:1003,"name":"xx","message":"1111"}") new.msg2= ws.recv() print (new.msg2)
附:
1、django安装:pip install requests;
2、配置django环境变量:D:\python\Scripts;
3、在工作空间目录下用django创建项目:django-admin startproject 项目名称;
目录结构如下:
├── HelloWorld │ ├── __init__.py │ ├── settings.py │ ├── urls.py │ └── wsgi.py ├── manage.py
4、Django提倡基于应用作为单位进行开发,创建djangoapp:进入HelloWorld目录下执行django-admin startapp say_hello命令;
├── HelloWorld │ ├── __init__.py │ ├── settings.py │ ├── urls.py │ └── wsgi.py ├── manage.py └── say_hello ├── admin.py ├── apps.py ├── __init__.py ├── migrations │ └── __init__.py ├── models.py ├── tests.py └── views.py
5、say_hello目录下,新建一个templates目录,其中创建LoginPage.html页面;
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>login</title> </head> <body> <form action="/say_hello/" method="POST"> #action后面是应用名称 <h1>用户名:<input name="username"></h1> <h1>密码:<input name="password"></h1> <input type="submit" value="登录"> </form>> </body> </html>
6、添加控制器:在say_hello目录下的views.py中添加函数;
使用render_to_response函数,该函数会返回一个对象,对象有经过渲染的html。
from django.http.response import HttpResponse from django.shortcuts import render_to_response def Login(request): if request.method ==‘POST‘: username = request.POST.get(‘username‘) return HttpResponse(username) else: return render_to_response(‘LoginPage.html‘)
7、设置一下url映射,打开HelloWorld目录下的urls.py,添加映射;
from django.contrib import admin from django.urls import path from say_hello import views from django.urls import re_path urlpatterns = [ path(‘admin/‘, admin.site.urls), re_path(r‘^login/‘,views.Login) ]
django2.0使用path,不是url,可以用re_path代替原来的url。若要用path可参考:https://www.cnblogs.com/feixuelove1009/p/8399338.html
8、注册应用,打开settings.py文件,在INSTALLED_APPS列表中添加一项"say_hello";
找不到templates目录需要添加:
‘DIRS‘: [os.path.join(BASE_DIR,‘templates‘)],
遇到安全问题需要注释:
#‘django.middleware.csrf.CsrfViewMiddleware‘,
9、启动服务器python manage.py runserver,在浏览器输入localhost:8000/login。
标签:sub encode har list shape 头信息 添加 input 映射
原文地址:https://www.cnblogs.com/cirr-zhou/p/9368396.html