首页 新闻 会员 周边

python 接口测试报错

0
悬赏园豆:10 [待解决问题]

"D:\Program Files (x86)\Python38\python.exe" D:/PycharmProjects/pythonProject/driver_api_auto/excel_load.py
Traceback (most recent call last):
File "D:\Program Files (x86)\Python38\lib\site-packages\urllib3\connection.py", line 158, in _new_conn
conn = connection.create_connection(
File "D:\Program Files (x86)\Python38\lib\site-packages\urllib3\util\connection.py", line 57, in create_connection
for res in socket.getaddrinfo(host, port, family, socket.SOCK_STREAM):
File "D:\Program Files (x86)\Python38\lib\socket.py", line 918, in getaddrinfo
for res in _socket.getaddrinfo(host, port, family, type, proto, flags):
socket.gaierror: [Errno 11001] getaddrinfo failed

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
File "D:\Program Files (x86)\Python38\lib\site-packages\urllib3\connectionpool.py", line 597, in urlopen
httplib_response = self._make_request(conn, method, url,
File "D:\Program Files (x86)\Python38\lib\site-packages\urllib3\connectionpool.py", line 343, in _make_request
self._validate_conn(conn)
File "D:\Program Files (x86)\Python38\lib\site-packages\urllib3\connectionpool.py", line 839, in _validate_conn
conn.connect()
File "D:\Program Files (x86)\Python38\lib\site-packages\urllib3\connection.py", line 301, in connect
conn = self._new_conn()
File "D:\Program Files (x86)\Python38\lib\site-packages\urllib3\connection.py", line 167, in _new_conn
raise NewConnectionError(
urllib3.exceptions.NewConnectionError: <urllib3.connection.VerifiedHTTPSConnection object at 0x047DFF40>: Failed to establish a new connection: [Errno 11001] getaddrinfo failed

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
File "D:\Program Files (x86)\Python38\lib\site-packages\requests\adapters.py", line 439, in send
resp = conn.urlopen(
File "D:\Program Files (x86)\Python38\lib\site-packages\urllib3\connectionpool.py", line 637, in urlopen
retries = retries.increment(method, url, error=e, _pool=self,
File "D:\Program Files (x86)\Python38\lib\site-packages\urllib3\util\retry.py", line 398, in increment
raise MaxRetryError(_pool, url, error or ResponseError(cause))
urllib3.exceptions.MaxRetryError: HTTPSConnectionPool(host='test-gw-driver.01zhuanche.com%0a', port=443): Max retries exceeded with url: /gw-driver/zhuanche-driver/driver/login (Caused by NewConnectionError('<urllib3.connection.VerifiedHTTPSConnection object at 0x047DFF40>: Failed to establish a new connection: [Errno 11001] getaddrinfo failed'))

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
File "D:/PycharmProjects/pythonProject/driver_api_auto/excel_load.py", line 54, in <module>
resp = send(url=url, method=method, headers=headers, **params)
File "D:\PycharmProjects\pythonProject\driver_api_auto\requests_client.py", line 19, in send
resp = session.request(url=url, method=method, **kwargs, verify=False, timeout=2)
File "D:\Program Files (x86)\Python38\lib\site-packages\requests\sessions.py", line 533, in request
resp = self.send(prep, **send_kwargs)
File "D:\Program Files (x86)\Python38\lib\site-packages\requests\sessions.py", line 646, in send
r = adapter.send(request, **kwargs)
File "D:\Program Files (x86)\Python38\lib\site-packages\requests\adapters.py", line 516, in send
raise ConnectionError(e, request=request)
requests.exceptions.ConnectionError: HTTPSConnectionPool(host='test-gw-driver.01zhuanche.com%0a', port=443): Max retries exceeded with url: /gw-driver/zhuanche-driver/driver/login (Caused by NewConnectionError('<urllib3.connection.VerifiedHTTPSConnection object at 0x047DFF40>: Failed to establish a new connection: [Errno 11001] getaddrinfo failed'))

Process finished with exit code 1

行者逍遥的主页 行者逍遥 | 初学一级 | 园豆:192
提问于:2022-06-02 11:24
< >
分享
所有回答(1)
0

首先

resp = session.request(url=url, method=method, **kwargs, verify=False, timeout=2)

# 你这个写法是不是有问题
# "默认传参"  应该放在 "**kwargs" 前面吧
resp = session.request(url=url, method=method,verify=False, timeout=2, **kwargs)

其次,打印显示底层依赖(site-packages)报错前,requests_client.py 是最后报错的位置,你自己 debug 一下

〆灬丶 | 园豆:2287 (老鸟四级) | 2022-06-02 11:37

requests_client.py 文件

import requests

import urllib3

urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning)
session = requests.session()

对于一个接口,url和请求方式一定是需要的,

但是注意headers、请求参数这些不一定有,所以我们使用**kwargs来表示不确定的参数

def send(url, method, **kwargs):
resp = session.request(url=url, method=method, verify=False, timeout=2, **kwargs)
# resp = session.request(url=url, method=method, **kwargs)
return resp

封装的一个方法,调用

支持(0) 反对(0) 行者逍遥 | 园豆:192 (初学一级) | 2022-06-02 11:43

@行者逍遥: ???

支持(0) 反对(0) 〆灬丶 | 园豆:2287 (老鸟四级) | 2022-06-02 11:46




支持(0) 反对(0) 行者逍遥 | 园豆:192 (初学一级) | 2022-06-02 11:50

@〆灬丶: requests_client.py 这个文件是封装,用来调用的
这是方法

def send(url, method, **kwargs):
resp = session.request(url=url, method=method, verify=False, timeout=2, **kwargs)

resp = session.request(url=url, method=method, **kwargs)

return resp

支持(0) 反对(0) 行者逍遥 | 园豆:192 (初学一级) | 2022-06-02 11:52

运行 print(resp.text) 后报错

File "D:\Program Files (x86)\Python38\lib\site-packages\requests\adapters.py", line 516, in send
raise ConnectionError(e, request=request)
requests.exceptions.ConnectionError: HTTPSConnectionPool(host='test-gw-driver.01zhuanche.com%0a', port=443): Max retries exceeded with url: /gw-driver/zhuanche-driver/driver/login (Caused by NewConnectionError('<urllib3.connection.VerifiedHTTPSConnection object at 0x047DFF40>: Failed to establish a new connection: [Errno 11001] getaddrinfo failed'))

Process finished with exit code 1

支持(0) 反对(0) 行者逍遥 | 园豆:192 (初学一级) | 2022-06-02 11:54

这一步debug resp = send(url=url, method=method, headers=headers, **params)debug结果:

支持(0) 反对(0) 行者逍遥 | 园豆:192 (初学一级) | 2022-06-02 11:57

@行者逍遥:
有两个疑问:
1.为啥不直接用 requests 去请求(另外,你可以先用 postman 去测试接口然后生成 Python 请求代码)
2. 最后一张截图,url 里为啥还有换行符

PS:同样的截图就不要重复发了,你发的累,我看着也累

支持(0) 反对(0) 〆灬丶 | 园豆:2287 (老鸟四级) | 2022-06-02 12:05

@〆灬丶: OK

支持(0) 反对(0) 行者逍遥 | 园豆:192 (初学一级) | 2022-06-02 12:13

我这个是写的Python 接口自动化的框架,postman感觉不适合 ;(Python 写的都是封装后,其他去调用封装;postman不知道怎么模拟这种场景。postman只用过单接口运行和关联接口操作。

文件

支持(0) 反对(0) 行者逍遥 | 园豆:192 (初学一级) | 2022-06-02 12:29

@行者逍遥: postman 是用来测试接口通不通的,起码能把url 带换行符的问题测出来

支持(0) 反对(0) 〆灬丶 | 园豆:2287 (老鸟四级) | 2022-06-02 13:37

@〆灬丶: 接口是通的,已经运行过了

支持(0) 反对(0) 行者逍遥 | 园豆:192 (初学一级) | 2022-06-02 13:43

@〆灬丶:

支持(0) 反对(0) 行者逍遥 | 园豆:192 (初学一级) | 2022-06-02 13:46

@行者逍遥: 意思是问题解决了?

支持(0) 反对(0) 〆灬丶 | 园豆:2287 (老鸟四级) | 2022-06-02 14:03

@〆灬丶: 我的问题是?
requests.exceptions.ConnectionError: HTTPSConnectionPool(host='test-gw-driver.01zhuanche.com%0a', port=443): Max retries exceeded with url: /gw-driver/zhuanche-driver/driver/login (Caused by NewConnectionError('<urllib3.connection.VerifiedHTTPSConnection object at 0x0476FF40>: Failed to establish a new connection: [Errno 11001] getaddrinfo failed'))

支持(0) 反对(0) 行者逍遥 | 园豆:192 (初学一级) | 2022-06-02 14:18

上面那个是接口的类单独运行的,接口没有问题。 运行的类调用其他类的时候报的错

支持(0) 反对(0) 行者逍遥 | 园豆:192 (初学一级) | 2022-06-02 14:20

@行者逍遥:

  1. 你前面说接口通了是指啥?你截图发的 url (带换行符的)能访问?
https://test-gw-driver.01zhuamche.com\n/gw-driver/zhuanche-driver/driver/login
  1. Failed to establish a new connection: [Errno 11001] getaddrinfo failed 这个报错,接口是怎么通的?前面都说链接失败了,这也能通?
支持(0) 反对(0) 〆灬丶 | 园豆:2287 (老鸟四级) | 2022-06-02 14:44

@行者逍遥: 另外,你反复强调接口没问题,我问的就是你贴的报错里面的 API HTTPSConnectionPool(host='test-gw-driver.01zhuanche.com%0a', port=443): Max retries exceeded with url: /gw-driver/zhuanche-driver/driver/login

支持(0) 反对(0) 〆灬丶 | 园豆:2287 (老鸟四级) | 2022-06-02 14:46

@〆灬丶: 报错文件里没有api,API在其他的类里

支持(0) 反对(0) 行者逍遥 | 园豆:192 (初学一级) | 2022-06-02 14:47

@〆灬丶: 单独运行接口文件没有问题。URL的换行符是调用api类的时候拼接生成的,不是直接请求url.

支持(0) 反对(0) 行者逍遥 | 园豆:192 (初学一级) | 2022-06-02 14:50

@〆灬丶:现在最大的问题是这个,其他的先等这个解决了再看。这个是重点

requests.exceptions.ConnectionError: HTTPSConnectionPool(host='test-gw-driver.01zhuanche.com%0a', port=443): Max retries exceeded with url: /gw-driver/zhuanche-driver/driver/login (Caused by NewConnectionError('<urllib3.connection.VerifiedHTTPSConnection object at 0x0476FF40>: Failed to establish a new connection: [Errno 11001] getaddrinfo failed'))

支持(0) 反对(0) 行者逍遥 | 园豆:192 (初学一级) | 2022-06-02 14:52
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册