|
| 1 | +#!/usr/bin/env python |
| 2 | +# encoding: utf-8 |
| 3 | + |
| 4 | +""" |
| 5 | +@author: zhanghe |
| 6 | +@software: PyCharm |
| 7 | +@file: test_request.py |
| 8 | +@time: 2017/4/26 上午9:55 |
| 9 | +""" |
| 10 | + |
| 11 | + |
| 12 | +import requests |
| 13 | +import time |
| 14 | + |
| 15 | + |
| 16 | +def test_01(): |
| 17 | + """ |
| 18 | + 测试请求延时 |
| 19 | + request start : 2017-04-26 10:05:56 |
| 20 | + request response: 2017-04-26 10:06:06 |
| 21 | + <Response [200]> |
| 22 | + process start:10:05:56 |
| 23 | + process end :10:06:06 |
| 24 | + :return: |
| 25 | + """ |
| 26 | + print 'request start :', time.strftime("%Y-%m-%d %H:%M:%S") |
| 27 | + res = requests.get('http://0:8899') |
| 28 | + print 'request response:', time.strftime("%Y-%m-%d %H:%M:%S") |
| 29 | + print res |
| 30 | + print res.text |
| 31 | + |
| 32 | + |
| 33 | +def test_02(): |
| 34 | + """ |
| 35 | + 测试响应超时 |
| 36 | + request start : 2017-04-26 10:11:16 |
| 37 | + HTTPConnectionPool(host='0', port=8899): Read timed out. (read timeout=2) |
| 38 | + request response: 2017-04-26 10:11:18 |
| 39 | + :return: |
| 40 | + """ |
| 41 | + print 'request start :', time.strftime("%Y-%m-%d %H:%M:%S") |
| 42 | + try: |
| 43 | + res = requests.get('http://0:8899', timeout=2) |
| 44 | + print res |
| 45 | + print res.text |
| 46 | + except requests.exceptions.ReadTimeout as e: |
| 47 | + print u'响应超时', e.message |
| 48 | + print 'request response:', time.strftime("%Y-%m-%d %H:%M:%S") |
| 49 | + |
| 50 | + |
| 51 | +def test_03(): |
| 52 | + """ |
| 53 | + 测试连接失败 |
| 54 | + request start : 2017-04-26 10:14:18 |
| 55 | + HTTPConnectionPool(host='0', port=9999): Max retries exceeded with url: / (Caused by NewConnectionError('<requests.packages.urllib3.connection.HTTPConnection object at 0x103e6f510>: Failed to establish a new connection: [Errno 61] Connection refused',)) |
| 56 | + request response: 2017-04-26 10:14:18 |
| 57 | + :return: |
| 58 | + """ |
| 59 | + print 'request start :', time.strftime("%Y-%m-%d %H:%M:%S") |
| 60 | + try: |
| 61 | + res = requests.get('http://0:9999') |
| 62 | + print res |
| 63 | + print res.text |
| 64 | + # 连接错误 |
| 65 | + except requests.exceptions.ConnectionError as e: |
| 66 | + print u'连接错误', e.message |
| 67 | + # 响应超时 |
| 68 | + except requests.exceptions.ReadTimeout as e: |
| 69 | + print u'响应超时', e.message |
| 70 | + print 'request response:', time.strftime("%Y-%m-%d %H:%M:%S") |
| 71 | + |
| 72 | + |
| 73 | +def test_04(): |
| 74 | + """ |
| 75 | + 测试连接成功,无响应 |
| 76 | + 测试过程: |
| 77 | + 开启监听:nc -l 9900 |
| 78 | + 测试请求 |
| 79 | + 断开监听 |
| 80 | + 测试结果: |
| 81 | + request start : 2017-04-26 11:24:12 |
| 82 | + 连接错误 ('Connection aborted.', error(54, 'Connection reset by peer')) |
| 83 | + request response: 2017-04-26 11:24:18 |
| 84 | + :return: |
| 85 | + """ |
| 86 | + print 'request start :', time.strftime("%Y-%m-%d %H:%M:%S") |
| 87 | + try: |
| 88 | + res = requests.get('http://0:9900') |
| 89 | + print res |
| 90 | + print res.text |
| 91 | + # 连接错误 |
| 92 | + except requests.exceptions.ConnectionError as e: |
| 93 | + print u'连接错误', e.message |
| 94 | + # 响应超时 |
| 95 | + except requests.exceptions.ReadTimeout as e: |
| 96 | + print u'响应超时', e.message |
| 97 | + print 'request response:', time.strftime("%Y-%m-%d %H:%M:%S") |
| 98 | + |
| 99 | + |
| 100 | +def test_05(): |
| 101 | + """ |
| 102 | + 测试连接超时 |
| 103 | + request start : 2017-04-26 11:36:20 |
| 104 | + 连接错误 HTTPConnectionPool(host='www.x.com', port=80): Max retries exceeded with url: / (Caused by ConnectTimeoutError(<requests.packages.urllib3.connection.HTTPConnection object at 0x1086d8750>, 'Connection to www.x.com timed out. (connect timeout=3)')) |
| 105 | + request response: 2017-04-26 11:36:23 |
| 106 | + :return: |
| 107 | + """ |
| 108 | + print 'request start :', time.strftime("%Y-%m-%d %H:%M:%S") |
| 109 | + try: |
| 110 | + res = requests.get('http://www.x.com', timeout=(3, 5)) |
| 111 | + print res |
| 112 | + print res.text |
| 113 | + # 连接错误 |
| 114 | + except requests.exceptions.ConnectionError as e: |
| 115 | + print u'连接错误', e.message |
| 116 | + # 响应超时 |
| 117 | + except requests.exceptions.ReadTimeout as e: |
| 118 | + print u'响应超时', e.message |
| 119 | + print 'request response:', time.strftime("%Y-%m-%d %H:%M:%S") |
| 120 | + |
| 121 | + |
| 122 | +def test_06(): |
| 123 | + """ |
| 124 | + 测试重试 |
| 125 | + 测试结果: |
| 126 | + request start : 2017-04-26 11:53:18 |
| 127 | + 连接错误 HTTPConnectionPool(host='www.x.com', port=80): Max retries exceeded with url: / (Caused by ConnectTimeoutError(<requests.packages.urllib3.connection.HTTPConnection object at 0x107691ed0>, 'Connection to www.x.com timed out. (connect timeout=3)')) |
| 128 | + request response: 2017-04-26 11:53:40 |
| 129 | + 结果分析: |
| 130 | + backoff_factor: 超时补偿(默认值0)sleep seconds:{backoff factor} * (2 ^ ({number of total retries} - 1)) |
| 131 | + 1、backoff_factor = 0.1 |
| 132 | + (5+1)*3 + (2**(1-1) + 2**(2-1) + 2**(3-1) + 2**(4-1) + 2**(5-1))*0.1 |
| 133 | + = 18 + 3.1 |
| 134 | + = 21.1 |
| 135 | +
|
| 136 | + 2、backoff_factor = 0 |
| 137 | + request start : 2017-04-26 12:51:44 |
| 138 | + 连接错误 HTTPConnectionPool(host='www.x.com', port=80): Max retries exceeded with url: / (Caused by ConnectTimeoutError(<requests.packages.urllib3.connection.HTTPConnection object at 0x10bae0ed0>, 'Connection to www.x.com timed out. (connect timeout=3)')) |
| 139 | + request response: 2017-04-26 12:52:02 |
| 140 | + (5+1)*3 |
| 141 | + = 18 |
| 142 | + :return: |
| 143 | + """ |
| 144 | + from requests.packages.urllib3.util.retry import Retry |
| 145 | + from requests.adapters import HTTPAdapter |
| 146 | + |
| 147 | + s = requests.Session() |
| 148 | + |
| 149 | + retries = Retry(total=5, |
| 150 | + backoff_factor=0.1, |
| 151 | + status_forcelist=[500, 502, 503, 504]) |
| 152 | + |
| 153 | + s.mount('http://', HTTPAdapter(max_retries=retries)) |
| 154 | + s.mount('https://', HTTPAdapter(max_retries=retries)) |
| 155 | + |
| 156 | + print 'request start :', time.strftime("%Y-%m-%d %H:%M:%S") |
| 157 | + try: |
| 158 | + res = s.get('http://www.x.com', timeout=(3, 5)) |
| 159 | + print res |
| 160 | + print res.text |
| 161 | + # 连接错误 |
| 162 | + except requests.exceptions.ConnectionError as e: |
| 163 | + print u'连接错误', e.message |
| 164 | + # 响应超时 |
| 165 | + except requests.exceptions.ReadTimeout as e: |
| 166 | + print u'响应超时', e.message |
| 167 | + print 'request response:', time.strftime("%Y-%m-%d %H:%M:%S") |
| 168 | + |
| 169 | + |
| 170 | +if __name__ == '__main__': |
| 171 | + # test_01() |
| 172 | + # test_02() |
| 173 | + # test_03() |
| 174 | + # test_04() |
| 175 | + # test_05() |
| 176 | + test_06() |
| 177 | + |
| 178 | + |
| 179 | +""" |
| 180 | +测试页面 |
| 181 | +index.php |
| 182 | +
|
| 183 | +<?php |
| 184 | +ini_set('date.timezone','Asia/Shanghai'); |
| 185 | +//打印时间 |
| 186 | +echo "process start:".date('h:i:s')."\n"; |
| 187 | +
|
| 188 | +//暂停 10 秒 |
| 189 | +sleep(10); |
| 190 | +
|
| 191 | +//打印时间 |
| 192 | +echo "process end :".date('h:i:s'); |
| 193 | +""" |
| 194 | + |
| 195 | +""" |
| 196 | +开启临时服务 |
| 197 | +php -S 0:8899 |
| 198 | +""" |
0 commit comments