Forums

Cloudinary

I`m having an error when using Cloudinary, the error is this:-

Unexpected error - MaxRetryError("HTTPSConnectionPool(host='api.cloudinary.com', port=443): Max retries exceeded with url: /v1_1/yaluwo/image/upload (Caused by NewConnectionError('<urllib3.connection.HTTPSConnection object at 0x7fcc64186400>: Failed to establish a new connection: [Errno 111] Connection refused'))")

I followed the toturial django-cloudinary-storage in pypi

Can you support me with this, when I tried this offline with my code on the local pc it worked, but when I upload it online I get this error?

The connection refused error suggests that the library that you're using is not proxy-aware. Free accounts on PythonAnywhere have to access the external Internet via a proxy server. That's normally invisible to you -- we put the details of the proxy into the system environment, and most HTTP libraries like requests will see those details and will use the proxy automatically. However, if you're using a library that does not look for the proxy information, it will try to connect directly and will get an error like that.

What is the full stack trace that you're getting for that error? Perhaps there will be some clues in there as to how to configure the library to use a proxy.

Installed Applications:

 ['django.contrib.admin',
     'django.contrib.auth',
     'django.contrib.contenttypes',
     'django.contrib.sessions',
     'django.contrib.messages',
     'django.contrib.staticfiles',
     'blog',
     'members',
     'ckeditor',
     'django_cleanup',
     'cloudinary_storage',
     'cloudinary']

Installed Middleware:

 ['django.middleware.security.SecurityMiddleware',
     'django.contrib.sessions.middleware.SessionMiddleware',
     'django.middleware.common.CommonMiddleware',
     'django.middleware.csrf.CsrfViewMiddleware',
     'django.contrib.auth.middleware.AuthenticationMiddleware',
     'django.contrib.messages.middleware.MessageMiddleware',
     'django.middleware.clickjacking.XFrameOptionsMiddleware']

Traceback:

File "/usr/lib/python3.8/site-packages/urllib3/connection.py" in _new_conn
  158.             conn = connection.create_connection(

File "/usr/lib/python3.8/site-packages/urllib3/util/connection.py" in create_connection
  80.         raise err

File "/usr/lib/python3.8/site-packages/urllib3/util/connection.py" in create_connection
  70.             sock.connect(sa)

During handling of the above exception ([Errno 111] Connection refused), another exception occurred:

File "/usr/lib/python3.8/site-packages/urllib3/connectionpool.py" in urlopen
  597.             httplib_response = self._make_request(conn, method, url,

File "/usr/lib/python3.8/site-packages/urllib3/connectionpool.py" in _make_request
  343.             self._validate_conn(conn)

File "/usr/lib/python3.8/site-packages/urllib3/connectionpool.py" in _validate_conn
  839.             conn.connect()

File "/usr/lib/python3.8/site-packages/urllib3/connection.py" in connect
  301.         conn = self._new_conn()

File "/usr/lib/python3.8/site-packages/urllib3/connection.py" in _new_conn
  167.             raise NewConnectionError(

During handling of the above exception (<urllib3.connection.VerifiedHTTPSConnection object at 0x7f05fc036b20>: Failed to establish a new connection: [Errno 111] Connection refused), another exception occurred:

File "/home/Yaluwo/.local/lib/python3.8/site-packages/cloudinary/uploader.py" in call_api
  454.             response = _http.request("POST", api_url, param_list, headers, **kw)

File "/usr/lib/python3.8/site-packages/urllib3/request.py" in request
  70.             return self.request_encode_body(method, url, fields=fields,

File "/usr/lib/python3.8/site-packages/urllib3/request.py" in request_encode_body
  150.         return self.urlopen(method, url, **extra_kw)

File "/usr/lib/python3.8/site-packages/urllib3/poolmanager.py" in urlopen
  324.             response = conn.urlopen(method, u.request_uri, **kw)

File "/usr/lib/python3.8/site-packages/urllib3/connectionpool.py" in urlopen
  663.             return self.urlopen(method, url, body, headers, retries,

File "/usr/lib/python3.8/site-packages/urllib3/connectionpool.py" in urlopen
  663.             return self.urlopen(method, url, body, headers, retries,

File "/usr/lib/python3.8/site-packages/urllib3/connectionpool.py" in urlopen
  663.             return self.urlopen(method, url, body, headers, retries,

File "/usr/lib/python3.8/site-packages/urllib3/connectionpool.py" in urlopen
  637.             retries = retries.increment(method, url, error=e, _pool=self,

File "/usr/lib/python3.8/site-packages/urllib3/util/retry.py" in increment
  399.             raise MaxRetryError(_pool, url, error or ResponseError(cause))

During handling of the above exception (HTTPSConnectionPool(host='api.cloudinary.com', port=443): Max retries exceeded with url: /v1_1/yaluwo/image/upload (Caused by NewConnectionError('<urllib3.connection.VerifiedHTTPSConnection object at 0x7f05fc036b20>: Failed to establish a new connection: [Errno 111] Connection refused'))), another exception occurred:

File "/usr/lib/python3.8/site-packages/django/core/handlers/exception.py" in inner
  34.             response = get_response(request)

File "/usr/lib/python3.8/site-packages/django/core/handlers/base.py" in _get_response
  115.                 response = self.process_exception_by_middleware(e, request)

File "/usr/lib/python3.8/site-packages/django/core/handlers/base.py" in _get_response
  113.                 response = wrapped_callback(request, *callback_args, **callback_kwargs)

File "/usr/lib/python3.8/site-packages/django/views/generic/base.py" in view
  71.             return self.dispatch(request, *args, **kwargs)

File "/usr/lib/python3.8/site-packages/django/contrib/auth/mixins.py" in dispatch
  52.         return super().dispatch(request, *args, **kwargs)

File "/usr/lib/python3.8/site-packages/django/views/generic/base.py" in dispatch
  97.         return handler(request, *args, **kwargs)

File "/usr/lib/python3.8/site-packages/django/views/generic/edit.py" in post
  172.         return super().post(request, *args, **kwargs)

File "/usr/lib/python3.8/site-packages/django/views/generic/edit.py" in post
  142.             return self.form_valid(form)

File "/usr/lib/python3.8/site-packages/django/views/generic/edit.py" in form_valid
  125.         self.object = form.save()

File "/usr/lib/python3.8/site-packages/django/forms/models.py" in save
  458.             self.instance.save()

File "/usr/lib/python3.8/site-packages/django/db/models/base.py" in save
  740.         self.save_base(using=using, force_insert=force_insert,

File "/usr/lib/python3.8/site-packages/django/db/models/base.py" in save_base
  777.             updated = self._save_table(

File "/usr/lib/python3.8/site-packages/django/db/models/base.py" in _save_table
  870.             result = self._do_insert(cls._base_manager, using, fields, update_pk, raw)

File "/usr/lib/python3.8/site-packages/django/db/models/base.py" in _do_insert
  907.         return manager._insert([self], fields=fields, return_id=update_pk,

File "/usr/lib/python3.8/site-packages/django/db/models/manager.py" in manager_method
  82.                 return getattr(self.get_queryset(), name)(*args, **kwargs)

File "/usr/lib/python3.8/site-packages/django/db/models/query.py" in _insert
  1186.         return query.get_compiler(using=using).execute_sql(return_id)

File "/usr/lib/python3.8/site-packages/django/db/models/sql/compiler.py" in execute_sql
  1334.             for sql, params in self.as_sql():

File "/usr/lib/python3.8/site-packages/django/db/models/sql/compiler.py" in as_sql
  1276.             value_rows = [

File "/usr/lib/python3.8/site-packages/django/db/models/sql/compiler.py" in <listcomp>
  1277.                 [self.prepare_value(field, self.pre_save_val(field, obj)) for field in fields]

File "/usr/lib/python3.8/site-packages/django/db/models/sql/compiler.py" in <listcomp>
  1277.                 [self.prepare_value(field, self.pre_save_val(field, obj)) for field in fields]

File "/usr/lib/python3.8/site-packages/django/db/models/sql/compiler.py" in pre_save_val
  1228.         return field.pre_save(obj, add=True)

File "/usr/lib/python3.8/site-packages/django/db/models/fields/files.py" in pre_save
  288.             file.save(file.name, file.file, save=False)

File "/usr/lib/python3.8/site-packages/django/db/models/fields/files.py" in save
  87.         self.name = self.storage.save(name, content, max_length=self.field.max_length)

File "/usr/lib/python3.8/site-packages/django/core/files/storage.py" in save
  52.         return self._save(name, content)

File "/home/Yaluwo/.local/lib/python3.8/site-packages/cloudinary_storage/storage.py" in _save
  68.         response = self._upload(name, content)

File "/home/Yaluwo/.local/lib/python3.8/site-packages/cloudinary_storage/storage.py" in _upload
  62.         return cloudinary.uploader.upload(content, **options)

File "/home/Yaluwo/.local/lib/python3.8/site-packages/cloudinary/uploader.py" in upload
  46.     return call_cacheable_api("upload", params, file=file, **options)

File "/home/Yaluwo/.local/lib/python3.8/site-packages/cloudinary/uploader.py" in call_cacheable_api
  392.     result = call_api(action, params, http_headers, return_error, unsigned, file, timeout, **options)

File "/home/Yaluwo/.local/lib/python3.8/site-packages/cloudinary/uploader.py" in call_api
  456.             raise Error("Unexpected error - {0!r}".format(e))

Exception Type: Error at /add_post/
Exception Value: Unexpected error - MaxRetryError("HTTPSConnectionPool(host='api.cloudinary.com', port=443): Max retries exceeded with url: /v1_1/yaluwo/image/upload (Caused by NewConnectionError('<urllib3.connection.VerifiedHTTPSConnection object at 0x7f05fc036b20>: Failed to establish a new connection: [Errno 111] Connection refused'))")

Request Method:

POST

Request URL:

https://yaluwo.pythonanywhere.com/add_post/

Django Version:

2.2.7

Exception Type:

Error

Exception Value:

Unexpected error - MaxRetryError("HTTPSConnectionPool(host='api.cloudinary.com', port=443): Max retries exceeded with url: /v1_1/yaluwo/image/upload (Caused by NewConnectionError('<urllib3.connection.VerifiedHTTPSConnection object at 0x7f05fc036b20>: Failed to establish a new connection: [Errno 111] Connection refused'))")

Exception Location:

/home/Yaluwo/.local/lib/python3.8/site-packages/cloudinary/uploader.py in call_api, line 456

Python Executable:

/usr/local/bin/uwsgi

Python Version:

3.8.0

Python Path:

['/home/Yaluwo/mysite',
 '/var/www',
 '.',
 '',
 '/var/www',
 '/usr/lib/python38.zip',
 '/usr/lib/python3.8',
 '/usr/lib/python3.8/lib-dynload',
 '/home/Yaluwo/.local/lib/python3.8/site-packages',
 '/usr/lib/python3.8/site-packages']

Server time:

Sun, 6 Jun 2021 04:26:20 +0000

USER

Charuth

GET

No GET data

POST

Variable    Value
csrfmiddlewaretoken 
'yTYUuNqEOnua3q1vIMcgONXPta5hSITSI8KVzl23xusemSBfC8YqGLoGDZj0RJXL'
title   
'IMG2'
author  
'1'
content 
'<p>IMG2</p>\r\n'

FILES

Variable    Value
image   
<InMemoryUploadedFile: icon.png (image/jpeg)>

COOKIES

Variable    Value
sessionid   
'm2w5yanm9vb1y3m24cxv6hlrfg6y121s'
csrftoken   
'GRnByVZRAaItkzUAX9Tk3Mdtb5zKTLSRQ69CDtBgjhGxD1ukRvFuVKEklUNtSMWK'

It looks like you need to set api_proxy in an environment variable to get it to work -- this GitHub issue has the details.