python的网络编程能力十分强大,其中python中的requests库宣言:HTTP for Humans (给人用的 HTTP 库)
在网络编程中,最基本的任务包含:
发送请求登录获取数据解析数据反序列化打印内容
目录:
一、安装
二、基本用法
三、URL传参/获取请求的URL/POST表单
四、HTTP状态码/重定向跳转/请求历史
五、请求头
六、响应头
七、响应内容
八、反序列JSON数据
九、Cookie
十、会话对象
十一、超时设置
十二、SSL证书验证
一、安装
pip install requests
二、基本用法
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'))
三、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'}
四、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
五、请求头
#!/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'}
六、响应头
#!/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'}
七、响应内容
#!/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
八、反序列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
九、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
输出结果如:
十、会话对象
会话对象让你能够跨请求保持某些参数。它也会在同一个 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
输出结果如下:
十一、超时设置
为防止服务器不能及时响应,大部分发至外部服务器的请求都应该带着 timeout 参数。如果没有 timeout,你的代码可能会挂起若干分钟甚至更长时间。
>>> r = requests.get('https://github.com', timeout=5)
>>> r = requests.get('https://github.com', timeout=None)
>>>
十二、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测试开发圈”,了解更多测试教程!***
转载于:https://www.cnblogs.com/guanfuchang/p/6139932.html
相关资源:python requests模块下载