InfluxDBListener - Getting 401 using with locust - python

Running a docker container InfluxDB database, and I'm trying to use the InfluxDBListener to send locust data to the database.
Here's my code:
#events.init.add_listener
def on_locust_init(environment, **_kwargs):
"""
Hook event that enables starting an influxdb connection
"""
bucket = 'load_bucket'
org = 'some_organization' # not used
token = 'zzzz_etc_etc_etc_==' # not used
# this settings matches the given docker-compose file
influxDBSettings = InfluxDBSettings(
influx_host='localhost',
influx_port=8086,
user='Load Tester',
pwd='some_password',
database=bucket
)
# start listener with the given configuration
InfluxDBListener(env=environment, influxDbSettings=influxDBSettings)
Error in locust log:
[2021-06-02 11:42:35,514] ILGUYL-LT1/ERROR/root: Could not connect to influxdb
Traceback (most recent call last):
File "c:\git\engine-load-tests\venv\lib\site-packages\locust_influxdb_listener\__init__.py", line 55, in __init__
self.influxdb_client.create_database(influxDbSettings.database)
File "c:\git\engine-load-tests\venv\lib\site-packages\influxdb\client.py", line 746, in create_database
self.query("CREATE DATABASE {0}".format(quote_ident(dbname)),
File "c:\git\engine-load-tests\venv\lib\site-packages\influxdb\client.py", line 521, in query
response = self.request(
File "c:\git\engine-load-tests\venv\lib\site-packages\influxdb\client.py", line 378, in request
raise InfluxDBClientError(err_msg, response.status_code)
influxdb.exceptions.InfluxDBClientError: 401: {"code":"unauthorized","message":"Unauthorized"}
The script which I am running is not in a docker container but just running locally.
Confirmed:
Influx is definitely running on localhost:8086. I can go to http://localhost:8086 on my computer to get to InfluxDB.
the bucket load_bucket already exists [noticed that the InfluxDBListener actually tries to create the DB]
user name and password are correct
In another script I wrote, I can do this with no problem:
with InfluxDBClient(url="http://localhost:8086", token=token, org=org) as _client:
with _client.write_api(write_options=WriteOptions(batch_size=500,
flush_interval=10_000,
jitter_interval=2_000,
retry_interval=5_000,
max_retries=5,
max_retry_delay=30_000,
exponential_base=2,
write_type=SYNCHRONOUS)) as _write_client:
After changing influx_host='localhost' to influx_host='influxdb', I get the following error:
Traceback (most recent call last):
File "c:\git\engine-load-tests\venv\lib\site-packages\urllib3\connection.py", line 169, in _new_conn
conn = connection.create_connection(
File "c:\git\engine-load-tests\venv\lib\site-packages\urllib3\util\connection.py", line 73, in create_connection
for res in socket.getaddrinfo(host, port, family, socket.SOCK_STREAM):
File "c:\git\engine-load-tests\venv\lib\site-packages\gevent\_socketcommon.py", line 247, in getaddrinfo
addrlist = get_hub().resolver.getaddrinfo(host, port, family, type, proto, flags)
File "c:\git\engine-load-tests\venv\lib\site-packages\gevent\resolver\thread.py", line 63, in getaddrinfo
return self.pool.apply(_socket.getaddrinfo, args, kwargs)
File "c:\git\engine-load-tests\venv\lib\site-packages\gevent\pool.py", line 161, in apply
return self.spawn(func, *args, **kwds).get()
File "src\\gevent\\event.py", line 329, in gevent._gevent_cevent.AsyncResult.get
File "src\\gevent\\event.py", line 359, in gevent._gevent_cevent.AsyncResult.get
File "src\\gevent\\event.py", line 347, in gevent._gevent_cevent.AsyncResult.get
File "src\\gevent\\event.py", line 327, in gevent._gevent_cevent.AsyncResult._raise_exception
File "c:\git\engine-load-tests\venv\lib\site-packages\gevent\_compat.py", line 65, in reraise
raise value.with_traceback(tb)
File "c:\git\engine-load-tests\venv\lib\site-packages\gevent\threadpool.py", line 167, in __run_task
thread_result.set(func(*args, **kwargs))
socket.gaierror: [Errno 11001] getaddrinfo failed
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "c:\git\engine-load-tests\venv\lib\site-packages\urllib3\connectionpool.py", line 699, in urlopen
httplib_response = self._make_request(
File "c:\git\engine-load-tests\venv\lib\site-packages\urllib3\connectionpool.py", line 394, in _make_request
conn.request(method, url, **httplib_request_kw)
File "c:\git\engine-load-tests\venv\lib\site-packages\urllib3\connection.py", line 234, in request
super(HTTPConnection, self).request(method, url, body=body, headers=headers)
File "C:\Python38\lib\http\client.py", line 1255, in request
self._send_request(method, url, body, headers, encode_chunked)
File "C:\Python38\lib\http\client.py", line 1301, in _send_request
self.endheaders(body, encode_chunked=encode_chunked)
File "C:\Python38\lib\http\client.py", line 1250, in endheaders
self._send_output(message_body, encode_chunked=encode_chunked)
File "C:\Python38\lib\http\client.py", line 1010, in _send_output
self.send(msg)
File "C:\Python38\lib\http\client.py", line 950, in send
self.connect()
File "c:\git\engine-load-tests\venv\lib\site-packages\urllib3\connection.py", line 200, in connect
conn = self._new_conn()
File "c:\git\engine-load-tests\venv\lib\site-packages\urllib3\connection.py", line 181, in _new_conn
raise NewConnectionError(
urllib3.exceptions.NewConnectionError: <urllib3.connection.HTTPConnection object at 0x00000221080B8C10>: 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 "c:\git\engine-load-tests\venv\lib\site-packages\requests\adapters.py", line 439, in send
resp = conn.urlopen(
File "c:\git\engine-load-tests\venv\lib\site-packages\urllib3\connectionpool.py", line 755, in urlopen
retries = retries.increment(
File "c:\git\engine-load-tests\venv\lib\site-packages\urllib3\util\retry.py", line 574, in increment
raise MaxRetryError(_pool, url, error or ResponseError(cause))
urllib3.exceptions.MaxRetryError: HTTPConnectionPool(host='influxdb', port=8086): Max retries exceeded with url: /query?q=CREATE+DATABASE+%22load_bucket%22&db=load_bucket (Caused by NewConnectionError('<urllib3.connection.HTTPConnectio
n object at 0x00000221080B8C10>: 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 "c:\git\engine-load-tests\venv\lib\site-packages\locust_influxdb_listener\__init__.py", line 55, in __init__
self.influxdb_client.create_database(influxDbSettings.database)
File "c:\git\engine-load-tests\venv\lib\site-packages\influxdb\client.py", line 746, in create_database
self.query("CREATE DATABASE {0}".format(quote_ident(dbname)),
File "c:\git\engine-load-tests\venv\lib\site-packages\influxdb\client.py", line 521, in query
response = self.request(
File "c:\git\engine-load-tests\venv\lib\site-packages\influxdb\client.py", line 332, in request
response = self._session.request(
File "c:\git\engine-load-tests\venv\lib\site-packages\requests\sessions.py", line 542, in request
resp = self.send(prep, **send_kwargs)
File "c:\git\engine-load-tests\venv\lib\site-packages\requests\sessions.py", line 655, in send
r = adapter.send(request, **kwargs)
File "c:\git\engine-load-tests\venv\lib\site-packages\requests\adapters.py", line 516, in send
raise ConnectionError(e, request=request)
requests.exceptions.ConnectionError: HTTPConnectionPool(host='influxdb', port=8086): Max retries exceeded with url: /query?q=CREATE+DATABASE+%22load_bucket%22&db=load_bucket (Caused by NewConnectionError('<urllib3.connection.HTTPConnec
tion object at 0x00000221080B8C10>: Failed to establish a new connection: [Errno 11001] getaddrinfo failed'))
Here is the influx section of the docker-compose.yml:
$ cat docker-compose.yml
influxdb:
image: influxdb:latest
container_name: influxdb
ports:
- "8083:8083"
- "8086:8086"
- "8090:8090"
env_file:
- 'env.influxdb'
volumes:
# Data persistency
# sudo mkdir -p /srv/docker/influxdb/data
- /srv/docker/influxdb/data:/var/lib/influxdb2
$ cat influxdb.conf
[default]
url = "http://localhost:8086"
token = "<my-token>"
org = "<my org name>"
active = true
$ cat env.influxdb
INFLUXDB_DATA_ENGINE=tsm1
INFLUXDB_REPORTING_DISABLED=false

Influx is definitely running on localhost:8086
Not quite. Assuming the InfluxDB service is called "influxdb" in your docker-compose.yml file, the db is running at influxdb:8086 which, if you expose port 8086, is visible as localhost:8086 on your docker host.
However, the container running your script does not see the exposed port as localhost, so in the python script localhost is the container where thy script runs, not the Docker host machine. You're attempting to connect to the local port 8086, which doesn't have a DB connection, and so the error.
The easiest solution is to replace influx_host='localhost' with influx_host='influxdb' (again with the assumption that influxdb is the name of the InfluxDB service you have in the docker-compose.yml file, edit as needed).

Related

HTTP request to prometheus sometimes refuses connection

I am using prometheus with scaphandre (docs) to measure energy consumption in my kubernetes cluster. In the guide the way they use to access prometheus data is to forward the port from the pod to the host, but I've seen it's slower than using a service and I've been having some issues with that too.
I've defined a service to access prometheus data:
# prometheus_service.yaml
apiVersion: v1
kind: Service
metadata:
name: prometheus-service
namespace: default
annotations:
prometheus.io/scrape: 'true'
prometheus.io/port: '9090'
spec:
selector:
app: prometheus
type: NodePort
ports:
- port: 9090
targetPort: 9090
nodePort: 30030
My problem is that I've been trying to get a query result from the URI: 192.168.131.180:30030/api/v1/query?query=scaph_host_power_microwatts, but it sometimes works fine and other times I get a connection refused error.
More precisely I have tried 3 ways to access it:
By using VSCode Thunder Client extension (a REST API extension that works like POSTMAN or Insomnia) and when I send the GET request 50% of the times I get Connection was refused by the server. and the other 50% I get a proper result.
By directly inputting the query in my browser, in this case I always get a result when accessing the URI (and I can tell it is not cached because the query result has a time attribute which is always different).
By making the query through a python program with the requests library (which is my main goal to go on with my app). In this case it also fails 50% of the time.
If the browser had also failed 50% of the times I would've thought that prometheus fails when no new data is available, but since the browser worked fine, there must be another reason.
For additional info: when I change the previous IP for localhost the same thing happens.
Is there a reason for this error?
PD: If you want the python traceback, I leave it here (sorry I wanted to write it in a kind of "See More" or "Expand Block" to avoid pasting it there but didn't know how):
Traceback (most recent call last):
File "/usr/lib/python3/dist-packages/urllib3/connection.py", line 159, in _new_conn
conn = connection.create_connection(
File "/usr/lib/python3/dist-packages/urllib3/util/connection.py", line 84, in create_connection
raise err
File "/usr/lib/python3/dist-packages/urllib3/util/connection.py", line 74, in create_connection
sock.connect(sa)
ConnectionRefusedError: [Errno 111] Connection refused
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "/usr/lib/python3/dist-packages/urllib3/connectionpool.py", line 665, in urlopen
httplib_response = self._make_request(
File "/usr/lib/python3/dist-packages/urllib3/connectionpool.py", line 387, in _make_request
conn.request(method, url, **httplib_request_kw)
File "/usr/lib/python3.8/http/client.py", line 1256, in request
self._send_request(method, url, body, headers, encode_chunked)
File "/usr/lib/python3.8/http/client.py", line 1302, in _send_request
self.endheaders(body, encode_chunked=encode_chunked)
File "/usr/lib/python3.8/http/client.py", line 1251, in endheaders
self._send_output(message_body, encode_chunked=encode_chunked)
File "/usr/lib/python3.8/http/client.py", line 1011, in _send_output
self.send(msg)
File "/usr/lib/python3.8/http/client.py", line 951, in send
self.connect()
File "/usr/lib/python3/dist-packages/urllib3/connection.py", line 187, in connect
conn = self._new_conn()
File "/usr/lib/python3/dist-packages/urllib3/connection.py", line 171, in _new_conn
raise NewConnectionError(
urllib3.exceptions.NewConnectionError: <urllib3.connection.HTTPConnection object at 0x7fe1d7788520>: Failed to establish a new connection: [Errno 111] Connection refused
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "/usr/lib/python3/dist-packages/requests/adapters.py", line 439, in send
resp = conn.urlopen(
File "/usr/lib/python3/dist-packages/urllib3/connectionpool.py", line 719, in urlopen
retries = retries.increment(
File "/usr/lib/python3/dist-packages/urllib3/util/retry.py", line 436, in increment
raise MaxRetryError(_pool, url, error or ResponseError(cause))
urllib3.exceptions.MaxRetryError: HTTPConnectionPool(host='192.168.131.180', port=30030): Max retries exceeded with url: /api/v1/query?query=scaph_host_power_microwatts (Caused by NewConnectionError('<urllib3.connection.HTTPConnection object at 0x7fe1d7788520>: Failed to establish a new connection: [Errno 111] Connection refused'))
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "pruebametrics.py", line 55, in <module>
foo()
File "pruebametrics.py", line 51, in foo
microwatts =requests.get("http://192.168.131.180:30030/api/v1/query", params={'query': 'scaph_host_power_microwatts'}).json()
File "/usr/lib/python3/dist-packages/requests/api.py", line 75, in get
return request('get', url, params=params, **kwargs)
File "/usr/lib/python3/dist-packages/requests/api.py", line 60, in request
return session.request(method=method, url=url, **kwargs)
File "/usr/lib/python3/dist-packages/requests/sessions.py", line 533, in request
resp = self.send(prep, **send_kwargs)
File "/usr/lib/python3/dist-packages/requests/sessions.py", line 646, in send
r = adapter.send(request, **kwargs)
File "/usr/lib/python3/dist-packages/requests/adapters.py", line 516, in send
raise ConnectionError(e, request=request)
requests.exceptions.ConnectionError: HTTPConnectionPool(host='192.168.131.180', port=30030): Max retries exceeded with url: /api/v1/query?query=scaph_host_power_microwatts (Caused by NewConnectionError('<urllib3.connection.HTTPConnection object at 0x7fe1d7788520>: Failed to establish a new connection: [Errno 111] Connection refused'))

Failed to generate list of vault tokens

Looking for a solution to the problem to identify the vault root tokens, I found the following documentation
https://www.greenreedtech.com/identifying-active-hashicorp-vault-root-tokens/
The script related in this link worked in a vault instance that I have in version 1.5.4, but i have a problem listing the vault tokens in version 1.6.1
Traceback (most recent call last):
File "/home/vault/tokenlist-prod.py", line 31, in <module>
output = client.lookup_token(key, accessor=True)
File "/usr/local/lib/python3.10/dist-packages/hvac/v1/__init__.py", line 444, in lookup_token
return self._adapter.post(path, json=accessor_param, wrap_ttl=wrap_ttl)
File "/usr/local/lib/python3.10/dist-packages/hvac/adapters.py", line 126, in post
return self.request("post", url, **kwargs)
File "/usr/local/lib/python3.10/dist-packages/hvac/adapters.py", line 364, in request
response = super(JSONAdapter, self).request(*args, **kwargs)
File "/usr/local/lib/python3.10/dist-packages/hvac/adapters.py", line 330, in request
utils.raise_for_error(
File "/usr/local/lib/python3.10/dist-packages/hvac/utils.py", line 41, in raise_for_error
raise exceptions.Forbidden(message, errors=errors, method=method, url=url)
hvac.exceptions.Forbidden: 1 error occurred:
* permission denied
, on post https://mydomain/v1/auth/token/lookup-accessor
python3 tokenlist-alter.py
Traceback (most recent call last):
File "/usr/lib/python3/dist-packages/urllib3/connection.py", line 174, in _new_conn
conn = connection.create_connection(
File "/usr/lib/python3/dist-packages/urllib3/util/connection.py", line 96, in create_connection
raise err
File "/usr/lib/python3/dist-packages/urllib3/util/connection.py", line 86, in create_connection
sock.connect(sa)
ConnectionRefusedError: [Errno 111] Connection refused
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "/usr/lib/python3/dist-packages/urllib3/connectionpool.py", line 704, in urlopen
httplib_response = self._make_request(
File "/usr/lib/python3/dist-packages/urllib3/connectionpool.py", line 399, in _make_request
conn.request(method, url, **httplib_request_kw)
File "/usr/lib/python3/dist-packages/urllib3/connection.py", line 239, in request
super(HTTPConnection, self).request(method, url, body=body, headers=headers)
File "/usr/lib/python3.10/http/client.py", line 1282, in request
self._send_request(method, url, body, headers, encode_chunked)
File "/usr/lib/python3.10/http/client.py", line 1328, in _send_request
self.endheaders(body, encode_chunked=encode_chunked)
File "/usr/lib/python3.10/http/client.py", line 1277, in endheaders
self._send_output(message_body, encode_chunked=encode_chunked)
File "/usr/lib/python3.10/http/client.py", line 1037, in _send_output
self.send(msg)
File "/usr/lib/python3.10/http/client.py", line 975, in send
self.connect()
File "/usr/lib/python3/dist-packages/urllib3/connection.py", line 205, in connect
conn = self._new_conn()
File "/usr/lib/python3/dist-packages/urllib3/connection.py", line 186, in _new_conn
raise NewConnectionError(
urllib3.exceptions.NewConnectionError: <urllib3.connection.HTTPConnection object at 0x7f398c14cd60>: Failed to establish a new connection: [Errno 111] Connection refused
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "/usr/lib/python3/dist-packages/requests/adapters.py", line 440, in send
resp = conn.urlopen(
File "/usr/lib/python3/dist-packages/urllib3/connectionpool.py", line 786, in urlopen
retries = retries.increment(
File "/usr/lib/python3/dist-packages/urllib3/util/retry.py", line 592, in increment
raise MaxRetryError(_pool, url, error or ResponseError(cause))
urllib3.exceptions.MaxRetryError: HTTPConnectionPool(host='localhost', port=54453): Max retries exceeded with url: /v1/auth/token/accessors?list=True (Caused by NewConnectionError('<urllib3.connection.HTTPConnection object at 0x7f398c14cd60>: Failed to establish a new connection: [Errno 111] Connection refused'))
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "/home/vault/tokenlist-alter.py", line 25, in <module>
payload = client.list('auth/token/accessors')
File "/usr/local/lib/python3.10/dist-packages/hvac/v1/__init__.py", line 253, in list
return self._adapter.get("/v1/{0}".format(path), params=payload)
File "/usr/local/lib/python3.10/dist-packages/hvac/adapters.py", line 113, in get
return self.request("get", url, **kwargs)
File "/usr/local/lib/python3.10/dist-packages/hvac/adapters.py", line 364, in request
response = super(JSONAdapter, self).request(*args, **kwargs)
File "/usr/local/lib/python3.10/dist-packages/hvac/adapters.py", line 313, in request
response = self.session.request(
File "/usr/lib/python3/dist-packages/requests/sessions.py", line 529, in request
resp = self.send(prep, **send_kwargs)
File "/usr/lib/python3/dist-packages/requests/sessions.py", line 645, in send
r = adapter.send(request, **kwargs)
File "/usr/lib/python3/dist-packages/requests/adapters.py", line 519, in send
raise ConnectionError(e, request=request)
requests.exceptions.ConnectionError: HTTPConnectionPool(host='localhost', port=54453): Max retries exceeded with url: /v1/auth/token/accessors?list=True (Caused by NewConnectionError('<urllib3.connection.HTTPConnection object at 0x7f398c14cd60>: Failed to establish a new connection: [Errno 111] Connection refused'))
The token I use to run the script is a root token
the urls i use have ssl certificate
Could someone help me with this problem?
Share my Script Script Py
Share Script
#!/usr/local/bin/python3
import os
import time
import hvac
import urllib3
from prettytable import PrettyTable
urllib3.disable_warnings()
try:
os.environ["VAULT_ADDR"] = "https://vault.test.com"
except Exception:
print("The VAULT_ADDR environment must be set.")
os._exit(1)
try:
os.environ["VAULT_TOKEN"] = "lalala"
except Exception:
print("The VAULT_TOKEN environment must be set.")
os._exit(1)
client = hvac.Client(url=os.environ['VAULT_ADDR'], verify=False, token=os.environ["VAULT_TOKEN"])
payload = client.list('auth/token/accessors')
keys = payload['data']['keys']
x = PrettyTable()
x.field_names = ["Display Name", "Creation Time", "Expiration Time", "Policies", "Token Accessor"]
for key in keys:
output = client.lookup_token(key, accessor=True)
display_name = output['data']['display_name']
creation_date = time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(output['data']['creation_time']))
expire_time = output['data']['expire_time']
policies = output['data']['policies']
accessor = key
if "root" in policies:
x.add_row([display_name, creation_date, expire_time, policies, accessor])
print(x)

Unable to run script through selenium grid. Got timeout error

Here is the log for Hub creation
Here is the log for Node creation
Here is Grid console output
Driver folder
This is the Python Script I wrote
import time
import unittest
from selenium import webdriver
class SearchProducts(unittest.TestCase):
def setUp(self):
desired_caps = {}
desired_caps['platform'] = 'WINDOWS'
desired_caps['browserName'] = 'chrome'
self.driver = webdriver.Remote('http://192.168.43.250:4444/wd/hub', desired_caps)
self.driver.get('http://demo.magentocommerce.com/')
self.driver.implicitly_wait(30)
self.driver.maximize_window()
def testSearchByCategory(self):
time.sleep(5)
# get the search textbox
self.search_icon = self.driver.find_element_by_xpath("//span[contains(., 'Search')]/preceding::span/div")
self.search_icon.click()
time.sleep(5)
self.search_field = self.driver.find_element_by_xpath("//div[#class='inputWrapper']//input[#id='edit-keys']")
self.search_field.click()
self.search_field.clear()
# enter search keyword and submit
self.search_field.send_keys('phones')
self.search_field.submit()
time.sleep(5)
def tearDown(self):
# close the browser window
self.driver.quit()
When ran script I got below log at Console
Error
Traceback (most recent call last):
File "C:\Users\jyothi\PycharmProjects\apautomationpython\venv\lib\site-packages\urllib3\connection.py", line 159, in _new_conn
conn = connection.create_connection(
File "C:\Users\jyothi\PycharmProjects\apautomationpython\venv\lib\site-packages\urllib3\util\connection.py", line 84, in create_connection
raise err
File "C:\Users\jyothi\PycharmProjects\apautomationpython\venv\lib\site-packages\urllib3\util\connection.py", line 74, in create_connection
sock.connect(sa)
TimeoutError: [WinError 10060] A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "C:\Users\PycharmProjects\apautomationpython\venv\lib\site-packages\urllib3\connectionpool.py", line 670, in urlopen
httplib_response = self._make_request(
File "C:\Users\PycharmProjects\apautomationpython\venv\lib\site-packages\urllib3\connectionpool.py", line 392, in _make_request
conn.request(method, url, **httplib_request_kw)
File "C:\Users\AppData\Local\Programs\Python\Python38\lib\http\client.py", line 1230, in request
self._send_request(method, url, body, headers, encode_chunked)
File "C:\Users\AppData\Local\Programs\Python\Python38\lib\http\client.py", line 1276, in _send_request
self.endheaders(body, encode_chunked=encode_chunked)
File "C:\Users\AppData\Local\Programs\Python\Python38\lib\http\client.py", line 1225, in endheaders
self._send_output(message_body, encode_chunked=encode_chunked)
File "C:\Users\AppData\Local\Programs\Python\Python38\lib\http\client.py", line 1004, in _send_output
self.send(msg)
File "C:\Users\AppData\Local\Programs\Python\Python38\lib\http\client.py", line 944, in send
self.connect()
File "C:\Users\PycharmProjects\apautomationpython\venv\lib\site-packages\urllib3\connection.py", line 187, in connect
conn = self._new_conn()
File "C:\Users\PycharmProjects\apautomationpython\venv\lib\site-packages\urllib3\connection.py", line 171, in _new_conn
raise NewConnectionError(
urllib3.exceptions.NewConnectionError: <urllib3.connection.HTTPConnection object at 0x000001A30BD477C0>: Failed to establish a new connection: [WinError 10060] A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "C:\Users\AppData\Local\Programs\Python\Python38\lib\unittest\case.py", line 60, in testPartExecutor
yield
File "C:\Users\AppData\Local\Programs\Python\Python38\lib\unittest\case.py", line 672, in run
self._callSetUp()
File "C:\Users\AppData\Local\Programs\Python\Python38\lib\unittest\case.py", line 630, in _callSetUp
self.setUp()
File "C:\Users\PycharmProjects\apautomationpython\src\test\automation\web\test.py", line 12, in setUp
self.driver = webdriver.Remote('http://192.168.43.250:4444/wd/hub', desired_caps)
File "C:\Users\PycharmProjects\apautomationpython\venv\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 157, in __init__
self.start_session(capabilities, browser_profile)
File "C:\Users\PycharmProjects\apautomationpython\venv\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 252, in start_session
response = self.execute(Command.NEW_SESSION, parameters)
File "C:\Users\PycharmProjects\apautomationpython\venv\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 319, in execute
response = self.command_executor.execute(driver_command, params)
File "C:\Users\PycharmProjects\apautomationpython\venv\lib\site-packages\selenium\webdriver\remote\remote_connection.py", line 374, in execute
return self._request(command_info[0], url, body=data)
File "C:\Users\PycharmProjects\apautomationpython\venv\lib\site-packages\selenium\webdriver\remote\remote_connection.py", line 402, in _request
resp = http.request(method, url, body=body, headers=headers)
File "C:\Users\PycharmProjects\apautomationpython\venv\lib\site-packages\urllib3\request.py", line 79, in request
return self.request_encode_body(
File "C:\Users\PycharmProjects\apautomationpython\venv\lib\site-packages\urllib3\request.py", line 171, in request_encode_body
return self.urlopen(method, url, **extra_kw)
File "C:\Users\PycharmProjects\apautomationpython\venv\lib\site-packages\urllib3\poolmanager.py", line 336, in urlopen
response = conn.urlopen(method, u.request_uri, **kw)
File "C:\Users\PycharmProjects\apautomationpython\venv\lib\site-packages\urllib3\connectionpool.py", line 752, in urlopen
return self.urlopen(
File "C:\Users\PycharmProjects\apautomationpython\venv\lib\site-packages\urllib3\connectionpool.py", line 752, in urlopen
return self.urlopen(
File "C:\Users\PycharmProjects\apautomationpython\venv\lib\site-packages\urllib3\connectionpool.py", line 752, in urlopen
return self.urlopen(
File "C:\Users\PycharmProjects\apautomationpython\venv\lib\site-packages\urllib3\connectionpool.py", line 724, in urlopen
retries = retries.increment(
File "C:\Users\PycharmProjects\apautomationpython\venv\lib\site-packages\urllib3\util\retry.py", line 439, in increment
raise MaxRetryError(_pool, url, error or ResponseError(cause))
urllib3.exceptions.MaxRetryError: HTTPConnectionPool(host='192.168.43.250', port=4444): Max retries exceeded with url: /wd/hub/session (Caused by NewConnectionError('<urllib3.connection.HTTPConnection object at 0x000001A30BD477C0>: Failed to establish a new connection: [WinError 10060] A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond'))
Everything seems fine to me but Whats going wrong? How to resolve this error? Please help

Prevent python script from stopping if the server is not connected

I have developed a python script which reads streams from multiple cameras and pass it to a server to display information on the frames. But the problem is that the script always fails if even of the ports of the server is not responding. I have tried exception handling but still no luck.
What I want is that if the server port is not responding then the frames must be displayed to the user without any information.
I have used python requests module.
Here is the code snippet:
self.frame = cv2.resize(frame, (self.screen_width, self.screen_height))
try:
payload = {
"frame":frame,
"timestamp": timestamp
}
payload = jsonpickle.encode(payload)
lock.acquire()
response = requests.post(self.frame_target_ip, data=payload, #self.frame_target_ip is the server ip with port number
headers=self.headers)
lock.release()
objects = jsonpickle.decode(response.content)
self.event_data, self.dets = self.parse_objects(objects)
if self.show_bounding_box == True:
draw_frame = self.draw_bounding_boxes(self.frame, self.dets)
self.frame = cv2.cvtColor(draw_frame, cv2.COLOR_BGR2RGB)
except Exception as e:
logging.error("Couldn't reach the frame_target_ip", exc_info=True)
I am using python 3 and windows 10.
Below is error traceback
00:31:52:327-root-INFO--Application Started
00:32:07:682-root-ERROR--Couldn't reach the frame_target_ip
Traceback (most recent call last):
File "C:\Users\Deepanshu\AppData\Local\Programs\Python\Python37\lib\site-packages\urllib3\connection.py", line 160, in _new_conn
(self._dns_host, self.port), self.timeout, **extra_kw
File "C:\Users\Deepanshu\AppData\Local\Programs\Python\Python37\lib\site-packages\urllib3\util\connection.py", line 84, in create_connection
raise err
File "C:\Users\Deepanshu\AppData\Local\Programs\Python\Python37\lib\site-packages\urllib3\util\connection.py", line 74, in create_connection
sock.connect(sa)
ConnectionRefusedError: [WinError 10061] No connection could be made because the target machine actively refused it
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "C:\Users\Deepanshu\AppData\Local\Programs\Python\Python37\lib\site-packages\urllib3\connectionpool.py", line 677, in urlopen
chunked=chunked,
File "C:\Users\Deepanshu\AppData\Local\Programs\Python\Python37\lib\site-packages\urllib3\connectionpool.py", line 392, in _make_request
conn.request(method, url, **httplib_request_kw)
File "C:\Users\Deepanshu\AppData\Local\Programs\Python\Python37\lib\http\client.py", line 1252, in request
self._send_request(method, url, body, headers, encode_chunked)
File "C:\Users\Deepanshu\AppData\Local\Programs\Python\Python37\lib\http\client.py", line 1298, in _send_request
self.endheaders(body, encode_chunked=encode_chunked)
File "C:\Users\Deepanshu\AppData\Local\Programs\Python\Python37\lib\http\client.py", line 1247, in endheaders
self._send_output(message_body, encode_chunked=encode_chunked)
File "C:\Users\Deepanshu\AppData\Local\Programs\Python\Python37\lib\http\client.py", line 1026, in _send_output
self.send(msg)
File "C:\Users\Deepanshu\AppData\Local\Programs\Python\Python37\lib\http\client.py", line 966, in send
self.connect()
File "C:\Users\Deepanshu\AppData\Local\Programs\Python\Python37\lib\site-packages\urllib3\connection.py", line 187, in connect
conn = self._new_conn()
File "C:\Users\Deepanshu\AppData\Local\Programs\Python\Python37\lib\site-packages\urllib3\connection.py", line 172, in _new_conn
self, "Failed to establish a new connection: %s" % e
urllib3.exceptions.NewConnectionError: <urllib3.connection.HTTPConnection object at 0x00000295D3350E48>: Failed to establish a new connection: [WinError 10061] No connection could be made because the target machine actively refused it
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "C:\Users\Deepanshu\AppData\Local\Programs\Python\Python37\lib\site-packages\requests\adapters.py", line 449, in send
timeout=timeout
File "C:\Users\Deepanshu\AppData\Local\Programs\Python\Python37\lib\site-packages\urllib3\connectionpool.py", line 725, in urlopen
method, url, error=e, _pool=self, _stacktrace=sys.exc_info()[2]
File "C:\Users\Deepanshu\AppData\Local\Programs\Python\Python37\lib\site-packages\urllib3\util\retry.py", line 439, in increment
raise MaxRetryError(_pool, url, error or ResponseError(cause))
urllib3.exceptions.MaxRetryError: HTTPConnectionPool(host='127.0.0.1', port=8000): Max retries exceeded with url: / (Caused by NewConnectionError('<urllib3.connection.HTTPConnection object at 0x00000295D3350E48>: Failed to establish a new connection: [WinError 10061] No connection could be made because the target machine actively refused it'))
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "c:/Users/Deepanshu/Desktop/VIDS_1cam/UI_1cam.py", line 221, in set_frame
headers=self.headers)
File "C:\Users\Deepanshu\AppData\Local\Programs\Python\Python37\lib\site-packages\requests\api.py", line 119, in post
return request('post', url, data=data, json=json, **kwargs)
File "C:\Users\Deepanshu\AppData\Local\Programs\Python\Python37\lib\site-packages\requests\api.py", line 61, in request
return session.request(method=method, url=url, **kwargs)
File "C:\Users\Deepanshu\AppData\Local\Programs\Python\Python37\lib\site-packages\requests\sessions.py", line 530, in request
resp = self.send(prep, **send_kwargs)
File "C:\Users\Deepanshu\AppData\Local\Programs\Python\Python37\lib\site-packages\requests\sessions.py", line 643, in send
r = adapter.send(request, **kwargs)
File "C:\Users\Deepanshu\AppData\Local\Programs\Python\Python37\lib\site-packages\requests\adapters.py", line 516, in send
raise ConnectionError(e, request=request)
requests.exceptions.ConnectionError: HTTPConnectionPool(host='127.0.0.1', port=8000): Max retries exceeded with url: / (Caused by NewConnectionError('<urllib3.connection.HTTPConnection object at 0x00000295D3350E48>: Failed to establish a new connection: [WinError 10061] No connection could be made because the target machine actively refused it'))

How to prevent the `requests` module error when no DNS resolution is available?

While using the Python requests module to check for broken links (expected behavior being returned error codes for unavailable URLs such as 404, etc.), I encountered an older link on a page for a domain which no longer exists, and a DNS error was returned.
<snip>
http://listen.grooveshark.com/s/Folk+Song/2Np5i?src=5
Traceback (most recent call last):
File "/usr/local/lib/python3.7/site-packages/urllib3/connection.py", line 171, in _new_conn
(self._dns_host, self.port), self.timeout, **extra_kw)
File "/usr/local/lib/python3.7/site-packages/urllib3/util/connection.py", line 56, in create_connection
for res in socket.getaddrinfo(host, port, family, socket.SOCK_STREAM):
File "/usr/local/Cellar/python/3.7.0/Frameworks/Python.framework/Versions/3.7/lib/python3.7/socket.py", line 748, in getaddrinfo
for res in _socket.getaddrinfo(host, port, family, type, proto, flags):
socket.gaierror: [Errno 8] nodename nor servname provided, or not known
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "/usr/local/lib/python3.7/site-packages/urllib3/connectionpool.py", line 600, in urlopen
chunked=chunked)
File "/usr/local/lib/python3.7/site-packages/urllib3/connectionpool.py", line 354, in _make_request
conn.request(method, url, **httplib_request_kw)
File "/usr/local/Cellar/python/3.7.0/Frameworks/Python.framework/Versions/3.7/lib/python3.7/http/client.py", line 1229, in request
self._send_request(method, url, body, headers, encode_chunked)
File "/usr/local/Cellar/python/3.7.0/Frameworks/Python.framework/Versions/3.7/lib/python3.7/http/client.py", line 1275, in _send_request
self.endheaders(body, encode_chunked=encode_chunked)
File "/usr/local/Cellar/python/3.7.0/Frameworks/Python.framework/Versions/3.7/lib/python3.7/http/client.py", line 1224, in endheaders
self._send_output(message_body, encode_chunked=encode_chunked)
File "/usr/local/Cellar/python/3.7.0/Frameworks/Python.framework/Versions/3.7/lib/python3.7/http/client.py", line 1016, in _send_output
self.send(msg)
File "/usr/local/Cellar/python/3.7.0/Frameworks/Python.framework/Versions/3.7/lib/python3.7/http/client.py", line 956, in send
self.connect()
File "/usr/local/lib/python3.7/site-packages/urllib3/connection.py", line 196, in connect
conn = self._new_conn()
File "/usr/local/lib/python3.7/site-packages/urllib3/connection.py", line 180, in _new_conn
self, "Failed to establish a new connection: %s" % e)
urllib3.exceptions.NewConnectionError: <urllib3.connection.HTTPConnection object at 0x1088ff748>: Failed to establish a new connection: [Errno 8] nodename nor servname provided, or not known
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "/usr/local/lib/python3.7/site-packages/requests/adapters.py", line 445, in send
timeout=timeout
File "/usr/local/lib/python3.7/site-packages/urllib3/connectionpool.py", line 638, in urlopen
_stacktrace=sys.exc_info()[2])
File "/usr/local/lib/python3.7/site-packages/urllib3/util/retry.py", line 398, in increment
raise MaxRetryError(_pool, url, error or ResponseError(cause))
urllib3.exceptions.MaxRetryError: HTTPConnectionPool(host='listen.grooveshark.com', port=80): Max retries exceeded with url: /s/Folk+Song/2Np5i?src=5 (Caused by NewConnectionError('<urllib3.connection.HTTPConnection object at 0x1088ff748>: Failed to establish a new connection: [Errno 8] nodename nor servname provided, or not known'))
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "./four_finder.py", line 64, in <module>
do_main()
File "./four_finder.py", line 48, in do_main
resp = requests.get(link)
File "/usr/local/lib/python3.7/site-packages/requests/api.py", line 72, in get
return request('get', url, params=params, **kwargs)
File "/usr/local/lib/python3.7/site-packages/requests/api.py", line 58, in request
return session.request(method=method, url=url, **kwargs)
File "/usr/local/lib/python3.7/site-packages/requests/sessions.py", line 512, in request
resp = self.send(prep, **send_kwargs)
File "/usr/local/lib/python3.7/site-packages/requests/sessions.py", line 622, in send
r = adapter.send(request, **kwargs)
File "/usr/local/lib/python3.7/site-packages/requests/adapters.py", line 513, in send
raise ConnectionError(e, request=request)
requests.exceptions.ConnectionError: HTTPConnectionPool(host='listen.grooveshark.com', port=80): Max retries exceeded with url: /s/Folk+Song/2Np5i?src=5 (Caused by NewConnectionError('<urllib3.connection.HTTPConnection object at 0x1088ff748>: Failed to establish a new connection: [Errno 8] nodename nor servname provided, or not known'))
[user#host:~/dev|22:13:13]
$ nslookup listen.grooveshark.com
(...)
** server can't find listen.grooveshark.com: SERVFAIL
[user#host:~/dev|22:28:05]
What is the best way to preempt this error from occurring? I can't be confident of future input URLs existing (as this example does not exist).
This is solved by using a try/except block and requests.ConnectionError. I hadn't searched SE for the right terms, and a useful answer is here.
try:
resp = requests.get(link)
except requests.ConnectionError as e:
### TODO: Properly handle this DNS error!
print('DNS ERROR: %s' % link)
continue
Also of interest is this answer.

Categories