How do i fix the error: Failed to establish a new connection: [Errno 111] Connection refused'))")
I have currently deployed my django-app via python anywhere.However despite proper configuration the error persist. I have reviewed possible solution on stack overflow but non seems to work.
Below is the code snippet in settings.py:
Here are server error.logs in python anywhere.:
here is further test that had done to debug that problem:
Answers
-
Kindly Assist,I feel this maybe a breakthrough to many devs
0 -
Hi @pat254,
You should be able to solve this issue by using the
api_proxy
parameter in your configuration. I'd recommend checking the following github issue which explains how to fix your issue.In that thread, it is advised to use this config:
import cloudinary cloudinary.config( cloud_name = "xxxx", api_key = "xxxxxxxxxxxx", api_secret = "xxxxxxxxxxx", api_proxy = "https://proxy.server:3128" ) import cloudinary.uploader import cloudinary.api
Please let us know how it goes.
0 -
Have followed every step stated in the GitHub and verified my credentials ..Also I added that proxy which pythonanywhere has recommended but still it's not working.N.B in my local PC it's working as desired but in the deployed app that's where am getting that error have double checked your recommendation with my work .
0 -
Hi @pat254
Please make sure you import the following:
import cloudinary.uploader
import cloudinary.api
after the configuration, can you please share your updated code?
Plus, depending on what plan you are on with pythonanywhere you may be throttled as this is one of the reasons that causes this. A way to find this out is to try connecting to other websites as well.
More on: https://stackoverflow.com/questions/71885891/urllib3-exceptions-maxretryerror-httpconnectionpoolhost-localhost-port-5958
You would need to contact the pythonanywhere team who would have access to the logs as to why the requests are being potentially throttled or blocked. Please let me know if you have any other questions or queries.
Kind Regards,
Tamara
0 -
Hi @Tamara,
.
Thank you for the insight , but as per this deployed app , I have already imported Cloudinary as well as the api
0 -
Hi @pat254,
So you have to import:
import cloudinary.uploader
import cloudinary.api
after the configuration, like that:
import cloudinary cloudinary.config( cloud_name = "xxxx", api_key = "xxxxxxxxxxxx", api_secret = "xxxxxxxxxxx", api_proxy = "https://proxy.server:3128") import cloudinary.uploader import cloudinary.api
Please try this and let me know how it goes.
Best,
Tamara
0 -
This is the settings.py file in the deployed app
import os
Initialize Cloudinary
from pathlib import Path
import cloudinary
import cloudinary.uploader
import cloudinary.apicloudinary.config(
cloud_name=os.getenv('CLOUDINARY_NAME', ' xxxx'),
api_key=os.getenv('CLOUDINARY_API_KEY', 'xxxxxx'),
api_secret=os.getenv('CLOUDINARY_API_SECRET', 'xxxxxxx'),
api_proxy=os.getenv('CLOUDINARY_API_PROXY', 'http://proxy.server:3128') # Proxy URL
)BASE_DIR = Path(file).resolve().parent.parent
Static and Media FilesMEDIA_ROOT = os.path.join(BASE_DIR, 'media')
Security and Debug
MEDIA_URL = '/media/'
STATIC_URL = '/static/'
STATICFILES_DIRS = [os.path.join(BASE_DIR, 'static')]
STATIC_ROOT = os.path.join(BASE_DIR, 'staticfiles')SECRET_KEY = os.getenv("DJANGO_SECRET_KEY", "your_default_secret_key")
Installed Apps
DEBUG = os.getenv("DJANGO_DEBUG", "False") == "True"
ALLOWED_HOSTS = ['kabau.pythonanywhere.com']INSTALLED_APPS = [
Cloudinary Configuration
"django.contrib.admin",
"django.contrib.auth",
"django.contrib.contenttypes",
"django.contrib.sessions",
"django.contrib.messages",
"django.contrib.staticfiles",
'rest_framework',
'rest_framework.authtoken',
'django_filters',
"corsheaders",
'authemail',
'accounts',
'achievemets',
'customer',
'entity',
'tickets',
'solutions',
'socials',
'comments',
'Benefit',
'Homeview',
'HeroPage',
'testimonials',
'license',
'users',
'cloudinary',
'cloudinary_storage',
'category',
'brands',
'colors',
'Enquiry',
'payments',
'product',
'FileUpload'
]CLOUDINARY_STORAGE = {
'CLOUD_NAME': os.getenv('CLOUDINARY_NAME', 'dkekd92kz'),
'API_KEY': os.getenv('CLOUDINARY_API_KEY', '347641118619835'),
'API_SECRET': os.getenv('CLOUDINARY_API_SECRET', 'DbTsY9xwBhcdOy4xndkAFHsnuXY'),
'API_PROXY': 'http://proxy.server:3128' # Proxy URL
}DEFAULT_FILE_STORAGE = 'cloudinary_storage.storage.MediaCloudinaryStorage'
Middleware and Other Settings
CLOUDINARY_URL = f"cloudinary://{os.getenv('CLOUDINARY_API_KEY')}:{os.getenv('CLOUDINARY_API_SECRET')}@{os.getenv('CLOUDINARY_NAME')}"MIDDLEWARE = [
"django.middleware.security.SecurityMiddleware",
"django.contrib.sessions.middleware.SessionMiddleware",
'corsheaders.middleware.CorsMiddleware',
"django.middleware.common.CommonMiddleware",
"django.middleware.csrf.CsrfViewMiddleware",
"django.contrib.auth.middleware.AuthenticationMiddleware",
"django.contrib.messages.middleware.MessageMiddleware",
"django.middleware.clickjacking.XFrameOptionsMiddleware",
]CORS_ALLOW_ALL_ORIGINS = True
Database Configuration
ROOT_URLCONF = "webbackend.urls"
WSGI_APPLICATION = "webbackend.wsgi.application"DATABASES = {
Password Validators
'default': {
'ENGINE': 'django.db.backends.mysql',
'NAME': 'xxxxxxx',
'USER': 'Kabau',
'PASSWORD': 'xxxxxx',
'HOST': 'xxxxx',
'PORT': '3306',
}
}AUTH_PASSWORD_VALIDATORS = [
Internationalization
{
"NAME": "django.contrib.auth.password_validation.UserAttributeSimilarityValidator",
},
{
"NAME": "django.contrib.auth.password_validation.MinimumLengthValidator",
},
{
"NAME": "django.contrib.auth.password_validation.CommonPasswordValidator",
},
{
"NAME": "django.contrib.auth.password_validation.NumericPasswordValidator",
},
]LANGUAGE_CODE = "en-us"
Default Auto Field
TIME_ZONE = "UTC"
USE_I18N = True
USE_TZ = TrueDEFAULT_AUTO_FIELD = "django.db.models.BigAutoField"
REST Framework ConfigurationREST_FRAMEWORK = {
Email Configuration
'DEFAULT_AUTHENTICATION_CLASSES': (
'rest_framework.authentication.TokenAuthentication',
),
'DEFAULT_PARSER_CLASSES': (
'rest_framework.parsers.JSONParser',
'rest_framework.parsers.MultiPartParser',
'rest_framework.parsers.FormParser',
),
}EMAIL_BACKEND = 'django.core.mail.backends.smtp.EmailBackend'
Custom User Model
EMAIL_HOST = 'smtp.gmail.com'
EMAIL_PORT = 587
EMAIL_USE_TLS = True
EMAIL_HOST_USER = os.getenv('EMAIL_HOST_USER', 'kabaupaatrick@gmail.com')
EMAIL_HOST_PASSWORD = os.getenv('EMAIL_HOST_PASSWORD', 'pwci uumf rbhj vjdg')AUTH_USER_MODEL = 'users.User'
0 -
This is the settings.py file in the deployed app
import osfrom pathlib import Pathimport cloudinaryimport cloudinary.uploaderimport cloudinary.api
Initialize Cloudinarycloudinary.config(cloud_name=os.getenv('CLOUDINARY_NAME', ' xxxx'),api_key=os.getenv('CLOUDINARY_API_KEY', 'xxxxxx'),api_secret=os.getenv('CLOUDINARY_API_SECRET', 'xxxxxxx'),api_proxy=os.getenv('CLOUDINARY_API_PROXY', 'http://proxy.server:3128') # Proxy URL)
BASE_DIR = Path(file).resolve().parent.parent
Static and Media FilesMEDIA_ROOT = os.path.join(BASE_DIR, 'media')MEDIA_URL = '/media/'STATIC_URL = '/static/'STATICFILES_DIRS = [os.path.join(BASE_DIR, 'static')]STATIC_ROOT = os.path.join(BASE_DIR, 'staticfiles')
Security and DebugSECRET_KEY = os.getenv("DJANGO_SECRET_KEY", "your_default_secret_key")DEBUG = os.getenv("DJANGO_DEBUG", "False") == "True"ALLOWED_HOSTS = ['kabau.pythonanywhere.com']
Installed AppsINSTALLED_APPS = ["django.contrib.admin","django.contrib.auth","django.contrib.contenttypes","django.contrib.sessions","django.contrib.messages","django.contrib.staticfiles",'rest_framework','rest_framework.authtoken','django_filters',"corsheaders",'authemail','accounts','achievemets','customer','entity','tickets','solutions','socials','comments','Benefit','Homeview','HeroPage','testimonials','license','users','cloudinary','cloudinary_storage','category','brands','colors','Enquiry','payments','product','FileUpload']
Cloudinary ConfigurationCLOUDINARY_STORAGE = {'CLOUD_NAME': os.getenv('CLOUDINARY_NAME', 'dkekd92kz'),'API_KEY': os.getenv('CLOUDINARY_API_KEY', '347641118619835'),'API_SECRET': os.getenv('CLOUDINARY_API_SECRET', 'DbTsY9xwBhcdOy4xndkAFHsnuXY'),'API_PROXY': 'http://proxy.server:3128' # Proxy URL}
DEFAULT_FILE_STORAGE = 'cloudinary_storage.storage.MediaCloudinaryStorage'CLOUDINARY_URL = f"cloudinary://{os.getenv('CLOUDINARY_API_KEY')}:{os.getenv('CLOUDINARY_API_SECRET')}@{os.getenv('CLOUDINARY_NAME')}"
Middleware and Other SettingsMIDDLEWARE = ["django.middleware.security.SecurityMiddleware","django.contrib.sessions.middleware.SessionMiddleware",'corsheaders.middleware.CorsMiddleware',"django.middleware.common.CommonMiddleware","django.middleware.csrf.CsrfViewMiddleware","django.contrib.auth.middleware.AuthenticationMiddleware","django.contrib.messages.middleware.MessageMiddleware","django.middleware.clickjacking.XFrameOptionsMiddleware",]
CORS_ALLOW_ALL_ORIGINS = TrueROOT_URLCONF = "webbackend.urls"WSGI_APPLICATION = "webbackend.wsgi.application"
Database ConfigurationDATABASES = {'default': {'ENGINE': 'django.db.backends.mysql','NAME': 'xxxxxxx','USER': 'Kabau','PASSWORD': 'xxxxxx','HOST': 'xxxxx','PORT': '3306',}}
Password ValidatorsAUTH_PASSWORD_VALIDATORS = [{"NAME": "django.contrib.auth.password_validation.UserAttributeSimilarityValidator",},{"NAME": "django.contrib.auth.password_validation.MinimumLengthValidator",},{"NAME": "django.contrib.auth.password_validation.CommonPasswordValidator",},{"NAME": "django.contrib.auth.password_validation.NumericPasswordValidator",},]
InternationalizationLANGUAGE_CODE = "en-us"TIME_ZONE = "UTC"USE_I18N = TrueUSE_TZ = True
Default Auto FieldDEFAULT_AUTO_FIELD = "django.db.models.BigAutoField"
REST Framework ConfigurationREST_FRAMEWORK = {'DEFAULT_AUTHENTICATION_CLASSES': ('rest_framework.authentication.TokenAuthentication',),'DEFAULT_PARSER_CLASSES': ('rest_framework.parsers.JSONParser','rest_framework.parsers.MultiPartParser','rest_framework.parsers.FormParser',),}
Email ConfigurationEMAIL_BACKEND = 'django.core.mail.backends.smtp.EmailBackend'EMAIL_HOST = 'smtp.gmail.com'EMAIL_PORT = 587EMAIL_USE_TLS = TrueEMAIL_HOST_USER = os.getenv('EMAIL_HOST_USER', 'kabaupaatrick@gmail.com')EMAIL_HOST_PASSWORD = os.getenv('EMAIL_HOST_PASSWORD', '')
Custom User ModelAUTH_USER_MODEL = 'users.User'
0 -
Hi @pat254
Thanks for those shared details.
Tamara mentioned trying this pattern of setting the config values before "import cloudinary.uploader" "import cloudinary.api" , but your example doesn't appear to follow this pattern.
Just so we can rule this out, can you try that, please?
Also, I want to confirm that after added the proxy there was no change to the errors that you're getting?0