python requests模塊使用

python的網絡編程能力十分強大,其中python中的requests庫宣言:HTTP for Humans (給人用的 HTTP 庫)html

在網絡編程中,最基本的任務包含:python

  • 發送請求
  • 登陸
  • 獲取數據
  • 解析數據
  • 反序列化打印內容

目錄:nginx

1、安裝git

2、基本用法github

3、URL傳參/獲取請求的URL/POST表單web

4、HTTP狀態碼/重定向跳轉/請求歷史編程

5、請求頭json

6、響應頭api

7、響應內容瀏覽器

8、反序列JSON數據

9、Cookie

10、會話對象

11、超時設置

12、SSL證書驗證

 

1、安裝

pip install requests

2、基本用法

import requests

cs_url = 'http://httpbin.org'

r = requests.get("%s/%s" % (cs_url, 'get'))
r = requests.post("%s/%s" % (cs_url, 'post'))
r = requests.put("%s/%s" % (cs_url, 'put'))
r = requests.delete("%s/%s" % (cs_url, 'delete'))
r = requests.patch("%s/%s" % (cs_url, 'patch'))
r = requests.options("%s/%s" % (cs_url, 'get'))

3、URL傳參/獲取請求的URL/POST表單

#!/usr/bin/env python 
# -*- coding: utf-8 -*-

import requests

payload = {'key1': 'value1', 'key2': 'value2'}

# URL 傳參
r = requests.get("http://httpbin.org/get", params=payload)
# 獲取請求的 URL
print "GET URL:", r.url

# POST 發送編碼爲表單形式的數據,requests 會自動將 Python 字典序列化爲實際的表單內容
r = requests.post("http://httpbin.org/post", data=payload)
# 獲取響應內容,string
print "POST Response:\n", r.content
if r.headers.get("content-type") == "application/json":
    # 獲取響應內容,dict類型
    print "r.json:\n", r.json()
    print "form:\n", r.json().get("form")

輸出結果:

GET URL: http://httpbin.org/get?key2=value2&key1=value1
POST Response:
{
  "args": {}, 
  "data": "", 
  "files": {}, 
  "form": {
    "key1": "value1", 
    "key2": "value2"
  }, 
  "headers": {
    "Accept": "*/*", 
    "Accept-Encoding": "gzip, deflate", 
    "Content-Length": "23", 
    "Content-Type": "application/x-www-form-urlencoded", 
    "Host": "httpbin.org", 
    "User-Agent": "python-requests/2.11.1"
  }, 
  "json": null, 
  "origin": "124.250.131.130", 
  "url": "http://httpbin.org/post"
}

r.json:
{u'files': {}, u'origin': u'124.250.131.130', u'form': {u'key2': u'value2', u'key1': u'value1'}, u'url': u'http://httpbin.org/post', u'args': {}, u'headers': {u'Content-Length': u'23', u'Accept-Encoding': u'gzip, deflate', u'Accept': u'*/*', u'User-Agent': u'python-requests/2.11.1', u'Host': u'httpbin.org', u'Content-Type': u'application/x-www-form-urlencoded'}, u'json': None, u'data': u''}
form:
{u'key2': u'value2', u'key1': u'value1'}

4、HTTP狀態碼/重定向跳轉/請求歷史

#!/usr/bin/env python 
# -*- coding: utf-8 -*-

import requests

url = "http://github.com"
# requests 默認自動地處理了 301/302 跳轉。在通過跳轉的請求中,返回的 URL 和狀態碼都是跳轉以後的信息;惟獨在 history 中,用 Python 列表記錄了跳轉狀況
r = requests.get("http://github.com")
print "request URL:", url
print "*"*60
print "默認狀況,response url:", r.url
print "默認狀況,response status code:", r.status_code
print "默認狀況,response history:", r.history
print "*"*60
# 有時候咱們也想單步追蹤頁面跳轉狀況。此時,能夠給請求加上 allow_redirects = False 參數。
r = requests.get("http://github.com", allow_redirects=False)
print "禁止自動跳轉後,response Uurl:", r.url
print "禁止自動跳轉後,response status code:", r.status_code
print "禁止自動跳轉後,response history:", r.history

5、請求頭

#!/usr/bin/env python 
# -*- coding: utf-8 -*-

import requests

cs_url = 'http://httpbin.org/get'
r = requests.get(cs_url)
# 查看請求頭
print "定製前:\n", r.request.headers
# 定製請求頭,HTTP 頭部是大小寫不敏感的,以下,User-Agent或user-agent都可
header = {
    # 微信UA
    'User-Agent': 'Mozilla/5.0 (Linux; Android 4.1.1; MI 2 Build/JRO03L) AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/37.0.0.0 Mobile MQQBrowser/6.8 TBS/036887 Safari/537.36 MicroMessenger/6.3.27.880 NetType/WIFI Language/zh_CN',
    # 用戶cookie
    'Cookie': 'gr_user_id=2e25dc22-6d3a-4190-86ce-2f0a04f357f5;PHPSESSID=786dcb5b1233dadc80817ea0e58a5de0',
    'Accept-Encoding': 'gzip',
}
r = requests.get(cs_url, headers=header)
# 查看請求頭
print "定製後:\n", r.request.headers

輸出結果:

定製前:
{'Connection': 'keep-alive', 'Accept-Encoding': 'gzip, deflate', 'Accept': '*/*', 'User-Agent': 'python-requests/2.11.1'}
定製後:
{'Connection': 'keep-alive', 'Cookie': 'gr_user_id=2e25dc22-6d3a-4190-86ce-2f0a04f357f5;PHPSESSID=786dcb5b1233dadc80817ea0e58a5de0', 'Accept-Encoding': 'gzip', 'Accept': '*/*', 'User-Agent': 'Mozilla/5.0 (Linux; Android 4.1.1; MI 2 Build/JRO03L) AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/37.0.0.0 Mobile MQQBrowser/6.8 TBS/036887 Safari/537.36 MicroMessenger/6.3.27.880 NetType/WIFI Language/zh_CN'}

6、響應頭

#!/usr/bin/env python
# -*- coding: utf-8 -*-

import requests

cs_url = 'http://httpbin.org/get'
r = requests.get(cs_url)
print r.headers

輸出結果:

{'Content-Length': '240', 'Server': 'nginx', 'Connection': 'keep-alive', 'Access-Control-Allow-Credentials': 'true', 'Date': 'Wed, 07 Dec 2016 06:22:51 GMT', 'Access-Control-Allow-Origin': '*', 'Content-Type': 'application/json'}

7、響應內容

#!/usr/bin/env python
# -*- coding: utf-8 -*-

import requests

cs_url = 'http://httpbin.org/get'
r = requests.get(cs_url)
if r.status_code == requests.codes.ok:
    print "以字節形式返回:\n", r.content
    print " Unicode編碼的文本信息返回:\n", r.text

8、反序列JSON數據

#!/usr/bin/env python
# -*- coding: utf-8 -*-

import requests

r = requests.get("http://httpbin.org/get")
if r.headers.get("content-type") == "application/json":
    # 獲取響應內容,dict類型
    print "r.json:\n", r.json()
    print "origin:", r.json().get("origin")
else:
    print r.content

9、Cookie

 HTTP 協議是無狀態的。所以,若不借助其餘手段,遠程的服務器就沒法知道之前和客戶端作了哪些通訊.Cookie就是「其餘手段」之一。

Cookie 一個典型的應用場景,就是用於記錄用戶在網站上的登陸狀態。
1.用戶登陸成功後,服務器下發一個(一般是加密了的)Cookie 文件。
2.客戶端(一般是網頁瀏覽器)將收到的 Cookie 文件保存起來。
3.下次客戶端與服務器鏈接時,將 Cookie 文件發送給服務器,由服務器校驗其含義,恢復登陸狀態(從而避免再次登陸)

爲了演示該實例,提供服務端簡單接口實例代碼以下:

# 使用cookie演示登陸功能接口
def cookie(request):
    username = request.GET.get("user")
    password = request.GET.get("pwd")
    cookie_content = request.COOKIES
    login_flag = cookie_content.get("is_login")
    if login_flag=="True" or (username == "qa" and password == "4399"):
        msg = {
            "msg": "login success! Welcome~~",
            "recive_cookie": cookie_content
        }
        response = JsonResponse(msg)
        response.set_cookie("is_login", True)
    else:
        msg = {
            "msg": "username or password error,please try again!",
            "recive_cookie": cookie_content
        }
        response = JsonResponse(msg)
        response.set_cookie("is_login", False)

    return response

cookie示例代碼以下,cookie既可經過requests的cookies參數傳遞,也能夠在requests的headers參數傳遞。(同時存在headers和cookies參數時,headers中的cookie會覆蓋cookies參數中的cookie)

#!/usr/bin/env python 
# -*- coding: utf-8 -*-


import requests

url = "http://10.1.102.75:8000/cookie"
r = requests.get(url)
print "沒有發送cookie時,服務端返回內容爲:", r.content
print "獲取服務端設置的cookie,cookie爲:", r.cookies
print "*" * 100

cookies = {
    "is_login": "True"
}
r = requests.get(url, cookies=cookies)
print "發送cookie時,服務端返回內容爲:", r.content
print "獲取服務端設置的cookie,cookie爲:", r.cookies
print "*" * 100

headers = {
    'cookie': 'send_headers=send cookie form client headers'
}
r = requests.get(url, headers=headers)
print "發送cookie時,服務端返回內容爲:", r.content
print "獲取服務端設置的cookie,cookie爲:", r.cookies
print "*" * 100

輸出結果如:

10、會話對象

 會話對象讓你可以跨請求保持某些參數。它也會在同一個 Session 實例發出的全部請求之間保持 cookie,因此若是你向同一主機發送多個請求,底層的 TCP 鏈接將會被重用,從而帶來顯著的性能提高。看本實例以前請先閱讀cookie章節

#!/usr/bin/env python 
# -*- coding: utf-8 -*-


import requests

login_url = "http://10.1.102.75:8000/cookie?user=qa&pwd=4399"
access_url = "http://10.1.102.75:8000/cookie"
s = requests.Session()
r = s.get(login_url)
print "傳入正確帳號與密碼正確登陸後,服務端返回內容爲:", r.content
print "獲取服務端設置的cookie,cookie爲:", r.cookies
print "*" * 100

r = s.get(access_url)
print "已登陸後,不發送已登陸cookie時或登陸帳號,訪問首頁時,服務端返回內容爲:", r.content
print "獲取服務端設置的cookie,cookie爲:", r.cookies
print "*" * 100

cookies = {
    "other_cookie": "test send order cookie"
}
r = s.get(access_url, cookies=cookies)
print "已登陸後,添加發送非登陸cookie,訪問首頁時服務端返回內容爲:", r.content
print "獲取服務端設置的cookie,cookie爲:", r.cookies
print "*" * 100

輸出結果以下:

11、超時設置

爲防止服務器不能及時響應,大部分發至外部服務器的請求都應該帶着 timeout 參數。若是沒有 timeout,你的代碼可能會掛起若干分鐘甚至更長時間。

>>> r = requests.get('https://github.com', timeout=5)
>>> r = requests.get('https://github.com', timeout=None)
>>>

12、SSL證書驗證

 Requests 能夠爲 HTTPS 請求驗證 SSL 證書,就像 web 瀏覽器同樣。要想檢查某個主機的 SSL 證書,你可使用 verify 參數:

>>> requests.get('https://github.com', verify=True)
<Response [200]>

默認狀況下, verify 是設置爲 True 的,若是SSL認證失敗,能夠嘗試將verify驗證關閉,verify=False

更多高級使用,請查看 http://docs.python-requests.org/zh_CN/latest/user/advanced.html

 1 #!/usr/bin/env python
 2 # -*- coding: utf-8 -*-
 3 
 4 """
 5 微信端抽獎活動
 6 多線程抽獎,測試前提:去掉一個用戶只能參與一次的限制
 7 """
 8 import requests
 9 from time import ctime
10 import threading
11 
12 
13 def draw_lottery():
14     lottery_url = "http://10.1.102.75:8000/activity/gsdzzlottery/lottery"
15     headers = {
16         # 微信UA
17         'user-agent': 'Mozilla/5.0 (Linux; Android 4.1.1; MI 2 Build/JRO03L) AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/37.0.0.0 Mobile MQQBrowser/6.8 TBS/036887 Safari/537.36 MicroMessenger/6.3.27.880 NetType/WIFI Language/zh_CN',
18         # 用戶cookie
19         'cookie': 'gr_user_id=2e25dc22-6d3a-4190-86ce-2f0a04f357f5; Hm_lvt_0d8e9cf3502496036a00d10b24863c6d=1478072857,1480158524; PHPSESSID=786dcb5b1233dadc80817ea0e58a5de0'
20     }
21     try:
22         conn = requests.get(lottery_url, headers=headers, verify=False)
23         print conn.text
24     except Exception, e:
25         print e
26 
27 
28 if __name__ == '__main__':
29     print 'start:', ctime()
30     for j in range(2):
31         threads = 300
32         threads_list = []
33         for i in range(threads):
34             t = threading.Thread(target=draw_lottery, args=())
35             threads_list.append(t)
36         for i in range(threads):
37             threads_list[i].start()
38             # keep thread
39         for i in range(threads):
40             threads_list[i].join()
41 
42     print 'end:', ctime()
結合threading,測試微信頁面併發抽獎小demo
#!/usr/bin/env python
# -*- coding: utf-8 -*-

"""
使用裝飾器,併發請求
"""
import requests
from time import ctime
import threading


def thread_request(count, loop):
    """併發請求裝飾器,count: 線程數 ; loop: 循環次數 """

    def outer(main_func):
        def inner():
            # print "before %s" % main_func
            for j in range(loop):
                threads_list = []
                for i in range(count):
                    t = threading.Thread(target=main_func, args=())
                    threads_list.append(t)
                for th in threads_list:
                    th.start()
                    # keep thread
                for th in threads_list:
                    th.join()
                    # print "after  %s" % main_func

        return inner

    return outer


@thread_request(count=2, loop=2)
def get_result():
    url = "http://a.demo.4399th.com/eventapi/base/get_result"
    data = {"hd_id": 1}
    headers = {
        # 微信UA
        # 'user-agent': 'Mozilla/5.0 (Linux; Android 4.1.1; MI 2 Build/JRO03L) AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/37.0.0.0 Mobile MQQBrowser/6.8 TBS/036887 Safari/537.36 MicroMessenger/6.3.27.880 NetType/WIFI Language/zh_CN',
        # 用戶cookie
        'cookie': 'a_demo_4399th_com=60a78048563a4b58e5d27d45390be36c'

    }
    try:
        conn = requests.post(url, data=data, headers=headers, verify=False)
        print conn.text
    except Exception, e:
        print e


@thread_request(count=2, loop=1)
def login():
    print "do login"


if __name__ == '__main__':
    print 'start:', ctime()
    get_result()
    print 'end:', ctime()
使用裝飾器,併發請求

 


***微信掃一掃,關注「python測試開發圈」,瞭解更多測試教程!***
相關文章
相關標籤/搜索