Connection error with `requests` on localhost with Docker - python

I'm trying to develop a client-server application but I'm running into some issues. Both my services are developed locally in a separate docker container.
The client fetches data from the api using requests python library. It works in production but locally, I can't use:
requests.get("http://localhost:PORT/ENDPOINT")
It keeps giving me back:
requests.exceptions.ConnectionError: HTTPConnectionPool(host='0.0.0.0', port=4000): Max retries exceeded with url: /data (Caused by NewConnectionError('<urllib3.connection.HTTPConnection object at 0x7f352ccd42b0>: Failed to establish a new connection: [Errno 111] Connection refused'))
Is there something I'm missing?

After further research, I found my answer here. Basically, when using Docker locally, you simply need to run:
requests.get("http://host.docker.internal:PORT/ENDPOINT")
instead of localhost

Related

How to run a Python 3 script in OWASP ZAP?

I'm using ZAP to run a scan of a website from the command line, using the form-based authentication script found in the ZAP API Documentation.
java -jar ./zap-2.11.1.jar -script ./auth_script.py
However, it looks like ZAP uses Jython 2.7 instead of Python 3, so running the script this way doesn't work.
I've also tried running the script directly (i.e. python3 auth_script.py), but it throws the following error:
requests.exceptions.ProxyError: HTTPConnectionPool(host='127.0.0.1', port=8080): Max retries exceeded with url: http://zap/JSON/context/action/includeInContext/?contextName=context&regex=https%3A%2F%2Fwebsite.com%2F&apikey=9qFbZD4udTzFVYo0u5UzkZX9iuzbdcJDRAquTfRk
(Caused by ProxyError('Cannot connect to proxy.', RemoteDisconnected('Remote end closed connection without response')))
Has anyone used this form-based authentication script before? How did you get it to work?

After Internet Outage, Python can't request API endpoints properly

Basically there was an internet outage yesterday, and I tried to run my FastAPI python backend which calls API endpoints, I was getting this error during the outage and wasn't surprised since I had no internet.
However, now that i've got internet back, the API's are still not requesting properly ( Other developers i'm working with who didn't face this outage have no problems)
requests.exceptions.ConnectTimeout: HTTPSConnectionPool(host='www.api.com', port=443): Max retries exceeded with url: /filter/professor/?&page=5&filter=teacherlastname_sort_s+asc&query=*%3A*&queryoption=TEACHER&queryBy=schoolId&sid=4714 (Caused by ConnectTimeoutError(<urllib3.connection.HTTPSConnection object at 0x00000184D99C0D00>, 'Connection to www.api.com timed out. (connect timeout=5)'))
What I tried:
Re-installed all pip requirements
Re-installing the repo ( New virtual environment)
I was trying to look online, and people say some config files might have corrupted, still unsure abt this

Python SSL Problem with PIP, Requests and Other

I've got a problem with a SSLError that appeared since last week.
I've used Python on my machine for a few years without any problem, but now whenever i try to use a library that connects to the web, a SSLError is thrown.
I've tried other solutions to make PIP and Requests work while avoiding the certificate check, but now i need to make it work to use an Azure library.
I know it's not a problem of the Wifi connection i'm using because it works fine on other machines. Could it be something i've installed on the machine? Maybe a VPN? Is there a way to check what is "blocking" the connection?
This is an example of the error when using the Azure Iot Hub library:
ClientRequestError: Error occurred in request., SSLError: HTTPSConnectionPool(host='iothubstreamdemo.azure-devices.net', port=443): Max retries exceeded with url: /devices?api-version=2020-03-01 (Caused by SSLError(SSLError(0, 'unknown error (_ssl.c:3622)'),))
And this is while using requests:
SSLError: HTTPSConnectionPool(host='example.com', port=443): Max retries exceeded with url: / (Caused by SSLError(SSLError(0, 'unknown error (_ssl.c:3622)'),))
Thanks in advance.

urllib3 request time out using inside ThreadPoolExecutor in flask app

I have a python script that works perfectly as a standalone module.
I am trying to use the output of that script in my flask app by initiating that module as a background job using concurrent.futures import ThreadPoolExecutor but unfortunately urllib3 gets failed with the following error only either I use ThreadPoolExecutor or Redis process for background task.
error with request HTTPSConnectionPool(host='www.reddit.com', port=443): Max retries exceeded with url: /api/v1/access_token (Caused by NewConnectionError('<urllib3.connection.HTTPSConnection object at 0x7fe8f9490a50>: Failed to establish a new connection: [Errno 2] Lookup timed out'))
That script is using praw library that further use urllib3 inside it. Is there any way I can run that module just like it works fine using the main thread?

Urllib3 Connection is timed out only in Azure Devops Build

I am using Azure Devops for CI/CD and encountered strange problem today. One of the python unit tests failed due to urllib3 connection time out. Here is the full output:
requests.exceptions.ConnectionError: HTTPSConnectionPool(host='goo.gl', port=443): Max retries exceeded with url: /maps/xxxxx (Caused by NewConnectionError('<urllib3.connection.VerifiedHTTPSConnection object at 0x000001AE5F442320>: 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',))
The strange thing is that; I logged in to build machine (it is a self-hosted Windows machine), tried to run same unit test with same configuration which azure devops build agent uses and test passed. By configuration I mean same conda environment and same Windows user which is "nt authority\network service" for azure devops build agent.
So here is the question why it is failing in the pipeline for no reason?

Categories