1 發送HTTP請求html
postman構建HTTP請求:python
1.1 requests模塊git
安裝:pip install requestsgithub
導入模塊:import requestsweb
1.2 請求與響應django
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")
請求後獲取經常使用的響應結果:json
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) #response = json.loads(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)
附:
一、django安裝:pip install requests;
二、配置django環境變量:D:\python\Scripts;
三、在工做空間目錄下用django建立項目:django-admin startproject 項目名稱;
目錄結構以下:
├── HelloWorld │ ├── __init__.py │ ├── settings.py │ ├── urls.py │ └── wsgi.py ├── manage.py
四、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
五、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>
六、添加控制器:在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')
七、設置一下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
八、註冊應用,打開settings.py文件,在INSTALLED_APPS列表中添加一項"say_hello";
找不到templates目錄須要添加:
'DIRS': [os.path.join(BASE_DIR,'templates')],
遇到安全問題須要註釋:
#'django.middleware.csrf.CsrfViewMiddleware',
九、啓動服務器python manage.py runserver,在瀏覽器輸入localhost:8000/login。