测试积点老人 发表于 2022-7-22 15:20:50

python脚本测springApi时报错

关于使用python脚本测springApi时报错的问题:
报错如下:
D:\pyth\python.exe D:/pythonProject/ApiTest.py
请求开始时间:request start time 2022-07-22 11:05:17.868684
Traceback (most recent call last):
File "D:\pyth\lib\site-packages\urllib3\connection.py", line 174, in _new_conn
    conn = connection.create_connection(
File "D:\pyth\lib\site-packages\urllib3\util\connection.py", line 95, in create_connection
    raise err
File "D:\pyth\lib\site-packages\urllib3\util\connection.py", line 85, in create_connection
    sock.connect(sa)
ConnectionRefusedError: 由于目标计算机积极拒绝,无法连接。

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
File "D:\pyth\lib\site-packages\urllib3\connectionpool.py", line 703, in urlopen
    httplib_response = self._make_request(
File "D:\pyth\lib\site-packages\urllib3\connectionpool.py", line 398, in _make_request
    conn.request(method, url, **httplib_request_kw)
File "D:\pyth\lib\site-packages\urllib3\connection.py", line 239, in request
    super(HTTPConnection, self).request(method, url, body=body, headers=headers)
File "D:\pyth\lib\http\client.py", line 1282, in request
    self._send_request(method, url, body, headers, encode_chunked)
File "D:\pyth\lib\http\client.py", line 1328, in _send_request
    self.endheaders(body, encode_chunked=encode_chunked)
File "D:\pyth\lib\http\client.py", line 1277, in endheaders
    self._send_output(message_body, encode_chunked=encode_chunked)
File "D:\pyth\lib\http\client.py", line 1037, in _send_output
    self.send(msg)
File "D:\pyth\lib\http\client.py", line 975, in send
    self.connect()
File "D:\pyth\lib\site-packages\urllib3\connection.py", line 205, in connect
    conn = self._new_conn()
File "D:\pyth\lib\site-packages\urllib3\connection.py", line 186, in _new_conn
    raise NewConnectionError(
urllib3.exceptions.NewConnectionError: at 0x0000016EAC334A60>: Failed to establish a new connection: 由于目标计算机积极拒绝,无法连接。
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "D:\pyth\lib\site-packages\requests\adapters.py", line 440, in send
    resp = conn.urlopen(
File "D:\pyth\lib\site-packages\urllib3\connectionpool.py", line 785, in urlopen
    retries = retries.increment(
File "D:\pyth\lib\site-packages\urllib3\util\retry.py", line 592, in increment
    raise MaxRetryError(_pool, url, error or ResponseError(cause))
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: 由于目标计算机积极拒绝,无法连接。')))
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "D:\pythonProject\ApiTest.py", line 130, in
    t = threading.Thread(target=myrequest.weather_DC())
File "D:\pythonProject\ApiTest.py", line 110, in weather_DC
    result = req.get(url=weatherinfo_search, headers=headers)
File "D:\pyth\lib\site-packages\requests\api.py", line 75, in get
    return request('get', url, params=params, **kwargs)
File "D:\pyth\lib\site-packages\requests\api.py", line 61, in request
    return session.request(method=method, url=url, **kwargs)
File "D:\pyth\lib\site-packages\requests\sessions.py", line 529, in request
    resp = self.send(prep, **send_kwargs)
File "D:\pyth\lib\site-packages\requests\sessions.py", line 645, in send
    r = adapter.send(request, **kwargs)
File "D:\pyth\lib\site-packages\requests\adapters.py", line 513, in send
    raise ProxyError(e, request=request)
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: 由于目标计算机积极拒绝,无法连接。')))
Process finished with exit code 1以下是脚本内容:
#coding=gbk
#coding:utf-8

import requests as req
import os
import datetime
import time
import threading

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

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


if __name__=='__main__':
    # 网络代理
    os.environ["http_proxy"] = "http://127.0.0.1:5500"
    os.environ["https_proxy"] = "http://127.0.0.1:5500"
    myrequest = url_request()
    threads = []
    starttime = datetime.datetime.now()
    print("请求开始时间:request start time %s" % starttime)
    nub = 10
    ThinkTime = 0.5
    for i in range(1, nub + 1):
      t = threading.Thread(target=myrequest.weather_DC())
      threads.append(t)
    for t in threads:
      time.sleep(ThinkTime)
      print("线程数:thread %s" % t)
      t.setDaemon(True)
      t.start()
      t.join()
    endtime = datetime.datetime.now()
    print("请求结束时间:request end time %s" % endtime)
    time.sleep(3)
    AverageTime = "{:.3f}".format(float(sum(myrequest.times)) / float(len(myrequest.times)))
    print("平均响应时间:Average Response Time %s ms" % AverageTime)
    usetime = str(endtime - starttime)
    hour = usetime.split(':').pop(0)
    minute = usetime.split(':').pop(1)
    second = usetime.split(':').pop(2)
    totaltime = float(hour) * 60 * 60 + float(minute) * 60 + float(second)
    print("并发数:Concurrent processing %s" % nub)
    print("#总共消耗的时间:use total time %s s" % (totaltime - float(nub * ThinkTime)))
    print("错误请求数:fail request %s s" % myrequest.error.count("0"))
    # test()请问需要如何配置才能正确测试该api

qqq911 发表于 2022-7-25 10:57:08

找运维开下网络权限

郭小贱 发表于 2022-7-25 13:58:56

Caused by ProxyError('Cannot connect to proxy.') 代理问题。

jingzizx 发表于 2022-7-25 14:23:39

没连上呢

bellas 发表于 2022-7-25 15:20:49

找下运维看下权限
页: [1]
查看完整版本: python脚本测springApi时报错