51Testing软件测试论坛

 找回密码
 (注-册)加入51Testing

QQ登录

只需一步,快速开始

微信登录,快人一步

手机号码,快捷登录

查看: 1032|回复: 4
打印 上一主题 下一主题

python脚本测springApi时报错

[复制链接]
  • TA的每日心情
    无聊
    4 天前
  • 签到天数: 530 天

    连续签到: 2 天

    [LV.9]测试副司令

    跳转到指定楼层
    #
    发表于 2022-7-22 15:20:50 | 只看该作者 回帖奖励 |正序浏览 |阅读模式
    1测试积点
    关于使用python脚本测springApi时报错的问题:
    报错如下:
    1. D:\pyth\python.exe D:/pythonProject/ApiTest.py
    2. 请求开始时间:request start time 2022-07-22 11:05:17.868684
    3. Traceback (most recent call last):
    4.   File "D:\pyth\lib\site-packages\urllib3\connection.py", line 174, in _new_conn
    5.     conn = connection.create_connection(
    6.   File "D:\pyth\lib\site-packages\urllib3\util\connection.py", line 95, in create_connection
    7.     raise err
    8.   File "D:\pyth\lib\site-packages\urllib3\util\connection.py", line 85, in create_connection
    9.     sock.connect(sa)
    10. ConnectionRefusedError: [WinError 10061] 由于目标计算机积极拒绝,无法连接。

    11. During handling of the above exception, another exception occurred:

    12. Traceback (most recent call last):
    13.   File "D:\pyth\lib\site-packages\urllib3\connectionpool.py", line 703, in urlopen
    14.     httplib_response = self._make_request(
    15.   File "D:\pyth\lib\site-packages\urllib3\connectionpool.py", line 398, in _make_request
    16.     conn.request(method, url, **httplib_request_kw)
    17.   File "D:\pyth\lib\site-packages\urllib3\connection.py", line 239, in request
    18.     super(HTTPConnection, self).request(method, url, body=body, headers=headers)
    19.   File "D:\pyth\lib\http\client.py", line 1282, in request
    20.     self._send_request(method, url, body, headers, encode_chunked)
    21.   File "D:\pyth\lib\http\client.py", line 1328, in _send_request
    22.     self.endheaders(body, encode_chunked=encode_chunked)
    23.   File "D:\pyth\lib\http\client.py", line 1277, in endheaders
    24.     self._send_output(message_body, encode_chunked=encode_chunked)
    25.   File "D:\pyth\lib\http\client.py", line 1037, in _send_output
    26.     self.send(msg)
    27.   File "D:\pyth\lib\http\client.py", line 975, in send
    28.     self.connect()
    29.   File "D:\pyth\lib\site-packages\urllib3\connection.py", line 205, in connect
    30.     conn = self._new_conn()
    31.   File "D:\pyth\lib\site-packages\urllib3\connection.py", line 186, in _new_conn
    32.     raise NewConnectionError(
    33. urllib3.exceptions.NewConnectionError: at 0x0000016EAC334A60>: Failed to establish a new connection: [WinError 10061] 由于目标计算机积极拒绝,无法连接。
    34. During handling of the above exception, another exception occurred:
    35. Traceback (most recent call last):
    36.   File "D:\pyth\lib\site-packages\requests\adapters.py", line 440, in send
    37.     resp = conn.urlopen(
    38.   File "D:\pyth\lib\site-packages\urllib3\connectionpool.py", line 785, in urlopen
    39.     retries = retries.increment(
    40.   File "D:\pyth\lib\site-packages\urllib3\util\retry.py", line 592, in increment
    41.     raise MaxRetryError(_pool, url, error or ResponseError(cause))
    42. urllib3.exceptions.MaxRetryError: HTTPConnectionPool(host='127.0.0.1', port=5500): Max retries exceeded with url: http://localhost:8080/sldndemo/test/admin/test/getImage (Caused by ProxyError('Cannot connect to proxy.', NewConnectionError(': Failed to establish a new connection: [WinError 10061] 由于目标计算机积极拒绝,无法连接。')))
    43. During handling of the above exception, another exception occurred:
    44. Traceback (most recent call last):
    45.   File "D:\pythonProject\ApiTest.py", line 130, in
    46.     t = threading.Thread(target=myrequest.weather_DC())
    47.   File "D:\pythonProject\ApiTest.py", line 110, in weather_DC
    48.     result = req.get(url=weatherinfo_search, headers=headers)
    49.   File "D:\pyth\lib\site-packages\requests\api.py", line 75, in get
    50.     return request('get', url, params=params, **kwargs)
    51.   File "D:\pyth\lib\site-packages\requests\api.py", line 61, in request
    52.     return session.request(method=method, url=url, **kwargs)
    53.   File "D:\pyth\lib\site-packages\requests\sessions.py", line 529, in request
    54.     resp = self.send(prep, **send_kwargs)
    55.   File "D:\pyth\lib\site-packages\requests\sessions.py", line 645, in send
    56.     r = adapter.send(request, **kwargs)
    57.   File "D:\pyth\lib\site-packages\requests\adapters.py", line 513, in send
    58.     raise ProxyError(e, request=request)
    59. requests.exceptions.ProxyError: HTTPConnectionPool(host='127.0.0.1', port=5500): Max retries exceeded with url: http://localhost:8080/sldndemo/test/admin/test/getImage (Caused by ProxyError('Cannot connect to proxy.', NewConnectionError(': Failed to establish a new connection: [WinError 10061] 由于目标计算机积极拒绝,无法连接。')))
    60. Process finished with exit code 1
    复制代码
    以下是脚本内容:

    1. #coding=gbk
    2. #coding:utf-8

    3. import requests as req
    4. import os
    5. import datetime
    6. import time
    7. import threading

    8. class url_request:
    9.     times = []
    10.     error = []
    11.     def weather_DC(self):
    12.         myrequest=url_request()
    13.         weatherinfo_search = 'http://localhost:8080/sldndemo/test/admin/test/getImage'
    14.         # 网络代理
    15.         os.environ["http_proxy"] = "http://127.0.0.1:5500"
    16.         os.environ["https_proxy"] = "http://127.0.0.1:5500"
    17.         # 请求标头
    18.         headers = {
    19.             "Accept": "application/json, text/javascript, */*; q=0.01"
    20.             , "Accept-Encoding": "gzip, deflate"
    21.             , "Accept-Language": "zh-CN,zh;q=0.9,en;q=0.8,en-GB;q=0.7,en-US;q=0.6"
    22.             , "Connection": "keep-alive"
    23.             , "Content-Length": "54"
    24.             , "Content-Type": "application/json; charset=UTF-8"
    25.             , "Host": "192.168.1.1"
    26.             , "Origin": ""
    27.             , "Referer": ""
    28.             , "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64)"
    29.                             " AppleWebKit/537.36 (KHTML, like Gecko) "
    30.                             "Chrome/103.0.5060.114 Edg/103.0.1264.62"
    31.             , "X-Requested-With": "XMLHttpRequest"
    32.             , "token": "。。。。"
    33.         }

    34.         result = req.get(url=weatherinfo_search, headers=headers)
    35.         print("状态码:",result.status_code)
    36.         print("返回报文:",result.text)
    37.         ResponseTime=float(result.elapsed.microseconds)/1000
    38.         myrequest.times.append(ResponseTime)
    39.         if result.status_code !=200 :
    40.             myrequest.error.append("0")


    41. if __name__=='__main__':
    42.     # 网络代理
    43.     os.environ["http_proxy"] = "http://127.0.0.1:5500"
    44.     os.environ["https_proxy"] = "http://127.0.0.1:5500"
    45.     myrequest = url_request()
    46.     threads = []
    47.     starttime = datetime.datetime.now()
    48.     print("请求开始时间:request start time %s" % starttime)
    49.     nub = 10
    50.     ThinkTime = 0.5
    51.     for i in range(1, nub + 1):
    52.         t = threading.Thread(target=myrequest.weather_DC())
    53.         threads.append(t)
    54.     for t in threads:
    55.         time.sleep(ThinkTime)
    56.         print("线程数:thread %s" % t)
    57.         t.setDaemon(True)
    58.         t.start()
    59.         t.join()
    60.     endtime = datetime.datetime.now()
    61.     print("请求结束时间:request end time %s" % endtime)
    62.     time.sleep(3)
    63.     AverageTime = "{:.3f}".format(float(sum(myrequest.times)) / float(len(myrequest.times)))
    64.     print("平均响应时间:Average Response Time %s ms" % AverageTime)
    65.     usetime = str(endtime - starttime)
    66.     hour = usetime.split(':').pop(0)
    67.     minute = usetime.split(':').pop(1)
    68.     second = usetime.split(':').pop(2)
    69.     totaltime = float(hour) * 60 * 60 + float(minute) * 60 + float(second)
    70.     print("并发数:Concurrent processing %s" % nub)
    71.     print("#总共消耗的时间:use total time %s s" % (totaltime - float(nub * ThinkTime)))
    72.     print("错误请求数:fail request %s s" % myrequest.error.count("0"))
    73.     # test()
    复制代码
    请问需要如何配置才能正确测试该api

    分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
    收藏收藏
    回复

    使用道具 举报

  • TA的每日心情
    奋斗
    昨天 10:15
  • 签到天数: 756 天

    连续签到: 1 天

    [LV.10]测试总司令

    4#
    发表于 2022-7-25 15:20:49 | 只看该作者
    找下运维看下权限
    回复

    使用道具 举报

  • TA的每日心情
    奋斗
    前天 07:50
  • 签到天数: 2818 天

    连续签到: 6 天

    [LV.Master]测试大本营

    3#
    发表于 2022-7-25 14:23:39 | 只看该作者
    没连上呢
    回复

    使用道具 举报

  • TA的每日心情
    奋斗
    2024-10-22 14:23
  • 签到天数: 1007 天

    连续签到: 1 天

    [LV.10]测试总司令

    2#
    发表于 2022-7-25 13:58:56 | 只看该作者
    Caused by ProxyError('Cannot connect to proxy.') 代理问题。
    回复

    使用道具 举报

  • TA的每日心情
    慵懒
    3 天前
  • 签到天数: 1521 天

    连续签到: 5 天

    [LV.Master]测试大本营

    1#
    发表于 2022-7-25 10:57:08 | 只看该作者
    找运维开下网络权限
    回复

    使用道具 举报

    本版积分规则

    关闭

    站长推荐上一条 /1 下一条

    小黑屋|手机版|Archiver|51Testing软件测试网 ( 沪ICP备05003035号 关于我们

    GMT+8, 2024-11-25 03:43 , Processed in 0.064142 second(s), 22 queries .

    Powered by Discuz! X3.2

    © 2001-2024 Comsenz Inc.

    快速回复 返回顶部 返回列表