Faile d to establish a new connection: [Errno 111] in Python
I am trying to upload images with Python (not Django) and I am running this script at PythonAnywhere. I have use this in my bash:
CLOUDINARY_URL=cloudinary://API_KEY:API_SECRET@DB?api_proxy=DONTKNOWWHATTOPUTHERE
And this is the error that throws me:
Traceback (most recent call last):
File "/home/gcancino/notionBooks.py", line 148, in <module>
queryWithFilter()
File "/home/gcancino/notionBooks.py", line 71, in queryWithFilter
getBookDataFromAPI(namee, pID)
File "/home/gcancino/notionBooks.py", line 93, in getBookDataFromAPI
upload("RitesofLucifer.jpg")
Traceback (most recent call last):
File "/home/gcancino/.local/lib/python3.9/site-packages/urllib3/connection.py", line 174, in _new_conn
conn = connection.create_connection(
File "/home/gcancino/.local/lib/python3.9/site-packages/urllib3/util/connection.py", line 95, in create_connection
raise err
File "/home/gcancino/.local/lib/python3.9/site-packages/urllib3/util/connection.py", line 85, 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 "/home/gcancino/.local/lib/python3.9/site-packages/cloudinary/uploader.py", line 534, in call_api
response = _http.request("POST", api_url, param_list, headers, **kw)
File "/home/gcancino/.local/lib/python3.9/site-packages/urllib3/request.py", line 78, in request
return self.request_encode_body(
File "/home/gcancino/.local/lib/python3.9/site-packages/urllib3/request.py", line 170, in request_encode_body
return self.urlopen(method, url, **extra_kw)
File "/home/gcancino/.local/lib/python3.9/site-packages/urllib3/poolmanager.py", line 375, in urlopen
response = conn.urlopen(method, u.request_uri, **kw)
File "/home/gcancino/.local/lib/python3.9/site-packages/urllib3/connectionpool.py", line 813, in urlopen
return self.urlopen(
File "/home/gcancino/.local/lib/python3.9/site-packages/urllib3/connectionpool.py", line 813, in urlopen
return self.urlopen(
File "/home/gcancino/.local/lib/python3.9/site-packages/urllib3/connectionpool.py", line 813, in urlopen
return self.urlopen(
File "/home/gcancino/.local/lib/python3.9/site-packages/urllib3/connectionpool.py", line 785, in urlopen
retries = retries.increment(
File "/home/gcancino/.local/lib/python3.9/site-packages/urllib3/util/retry.py", line 592, in increment
raise MaxRetryError(_pool, url, error or ResponseError(cause))
urllib3.exceptions.MaxRetryError: TCPKeepAliveHTTPSConnectionPool(host='api.cloudinary.com', port=443): Max retries exceeded with url: /v1_1/sampl
e/image/upload (Caused by NewConnectionError('<urllib3.connection.HTTPSConnection object at 0x7f7eb1283280>: Failed to establish a new connection: [Errno 111] Connection refused'))
And my code is this:
import cloudinary
from cloudinary.uploader import upload
from cloudinary.utils import cloudinary_url
cloudinary.config(
cloud_name = "name",
api_key = "API_KEY",
api_secret = "API_SECURE",
secure = True
)
upload("BookCoverTest.jpg")
Could you tell me what am I doing wrong?
Thanks in advance.
-
Hi Genaro,
I've found this StackOverflow thread of what seems to be the exact issue you're describing -
https://stackoverflow.com/questions/54217041/how-to-set-up-proxy-details-in-pythonanywhere-for-cloudinary
Can you try upgrading theurllib3
library and let me know if it helps?
Another possible solution I found is setting theapi_proxy
attribute in the Cloudinary configuration object tohttp://proxy.server:3128
CLOUDINARY = {
'cloud_name': '',
'api_key': '',
'api_secret': '',
'api_proxy': 'http://proxy.server:3128'
}Can you try this solution as well?
Thanks,Wissam
-
Hello, Wissam
Thanks for your response.
I tried to upgrade the lib in the server and put the proxy in the config with no result.Still getting the same errorDuring handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "/home/gcancino/.local/lib/python3.9/site-packages/cloudinary/uploader.py", line 534, in call_api
response = _http.request("POST", api_url, param_list, headers, **kw)
File "/home/gcancino/.local/lib/python3.9/site-packages/urllib3/request.py", line 78, in request
return self.request_encode_body(
File "/home/gcancino/.local/lib/python3.9/site-packages/urllib3/request.py", line 170, in request_encode_body
return self.urlopen(method, url, **extra_kw)
File "/home/gcancino/.local/lib/python3.9/site-packages/urllib3/poolmanager.py", line 375, in urlopen
response = conn.urlopen(method, u.request_uri, **kw)
File "/home/gcancino/.local/lib/python3.9/site-packages/urllib3/connectionpool.py", line 813, in urlopen
return self.urlopen(
File "/home/gcancino/.local/lib/python3.9/site-packages/urllib3/connectionpool.py", line 813, in urlopen
return self.urlopen(
File "/home/gcancino/.local/lib/python3.9/site-packages/urllib3/connectionpool.py", line 813, in urlopen
return self.urlopen(
File "/home/gcancino/.local/lib/python3.9/site-packages/urllib3/connectionpool.py", line 785, in urlopen
retries = retries.increment(
File "/home/gcancino/.local/lib/python3.9/site-packages/urllib3/util/retry.py", line 592, in increment
raise MaxRetryError(_pool, url, error or ResponseError(cause))
urllib3.exceptions.MaxRetryError: TCPKeepAliveHTTPSConnectionPool(host='api.cloudinary.com', port=443): Max retries exceeded with url: /v1_1/dorz1
6tij/image/upload (Caused by NewConnectionError('<urllib3.connection.HTTPSConnection object at 0x7f7f7fac8b80>: Failed to establish a new connecti
on: [Errno 111] Connection refused')) -
Hi Genaro,
I saw that it was resolved in the following thread:
settings.py should be in lower caps.
CLOUDINARY = { 'cloud_name': 'CLOUD-NAME', 'api_key': 'xx', 'api_secret': 'xx', 'api_proxy': 'http://proxy.server:3128' }
This is what I found from the Pythoanywhere forum:
https://help.pythonanywhere.com/pages/403ForbiddenError
Regards,
Wissam
-
Hey again, Wissam.
The problem is that I am not using django. I am just using Python.
So I put my config like this:
cloudinary.config(
cloud_name = xxxx",
api_key = "xxxxxxxxxxxx",
api_secret = "xxxxxxxxxxx",
api_proxy = "http://proxy.server:3128"
) -
Hi Genaro,
Just a quick update, I tested this myself on a PythonEverwhere instance (Free version) and I was able to replicate it. I'm still unsure what the reason is but right now it could be something to do with our SDK as making a direct call to our APIs were successful for me. The instance should already be configured to use their Proxy from initial investigations.
I will look into it more but do let still let us know the PythonEverywhere plan you are on. For now, a workaround is to build the request yourself.
More on manual generation here. Let us know if you have any questions in the meantime.
Thanks,
Thomas -
Hi Genaro,
Apologies for the delay, so I've investigated this a bit and can you instead create an environment variable like so before running the script:
export CLOUDINARY_URL=cloudinary://API_KEY:API_SECRET@cloud_name?api_proxy=http://proxy.server:3128
You'll probably want to save that to your .bashrc file so it pre-loads every time and you don't have to manually set it.
echo "export CLOUDINARY_URL=cloudinary://API_KEY:API_SECRET@cloud_name?api_proxy=
http://proxy.server:3128
" >> ~/.bashrc
source ~/.bashrcIf you prefer using cloudinary.config() then make sure to include it after "import cloudinary" module but before "import cloudinary.uploader" and "import cloudinary.api". So something like:
import cloudinary
cloudinary.config(
cloud_name = "xxxx",
api_key = "xxxxxxxxxxxx",
api_secret = "xxxxxxxxxxx",
api_proxy = "http://proxy.server:3128"
)
import cloudinary.uploader
import cloudinary.apiPlease let me know how you get on.
Thanks,
Thomas -
I was facing the same issue and it was resolved by writing this exact same code snippet.
import cloudinary
cloudinary.config(
cloud_name = config('CLOUD_NAME'),
api_key = config('CLOUD_API_KEY'),
api_secret = config('CLOUD_API_SECRET'),
api_proxy = "http://proxy.server:3128"
)
import cloudinary.uploader
import cloudinary.apiThanks Thomas,
Post is closed for comments.
Comments
7 comments