How do i fix the error: Failed to establish a new connection: [Errno 111] Connection refused'))")

pat254
pat254 Member Posts: 7

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:

Tagged:

Answers

  • pat254
    pat254 Member Posts: 7

    Kindly Assist,I feel this maybe a breakthrough to many devs

  • johnr
    johnr Member Posts: 14

    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.

  • pat254
    pat254 Member Posts: 7

    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 .

  • Tamara
    Tamara Member, Cloudinary Staff Posts: 123

    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

  • pat254
    pat254 Member Posts: 7

    Hi @Tamara,

    .

    Thank you for the insight , but as per this deployed app , I have already imported Cloudinary as well as the api

  • Tamara
    Tamara Member, Cloudinary Staff Posts: 123

    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

  • pat254
    pat254 Member Posts: 7

    This is the settings.py file in the deployed app

    import os
    from pathlib import Path
    import cloudinary
    import cloudinary.uploader
    import cloudinary.api

    Initialize Cloudinary

    cloudinary.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 Files

    MEDIA_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 Debug

    SECRET_KEY = os.getenv("DJANGO_SECRET_KEY", "your_default_secret_key")
    DEBUG = os.getenv("DJANGO_DEBUG", "False") == "True"
    ALLOWED_HOSTS = ['kabau.pythonanywhere.com']

    Installed Apps

    INSTALLED_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 Configuration

    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'
    CLOUDINARY_URL = f"cloudinary://{os.getenv('CLOUDINARY_API_KEY')}:{os.getenv('CLOUDINARY_API_SECRET')}@{os.getenv('CLOUDINARY_NAME')}"

    Middleware and Other Settings

    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
    ROOT_URLCONF = "webbackend.urls"
    WSGI_APPLICATION = "webbackend.wsgi.application"

    Database Configuration

    DATABASES = {
    'default': {
    'ENGINE': 'django.db.backends.mysql',
    'NAME': 'xxxxxxx',
    'USER': 'Kabau',
    'PASSWORD': 'xxxxxx',
    'HOST': 'xxxxx',
    'PORT': '3306',
    }
    }

    Password Validators

    AUTH_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",
    },
    ]

    Internationalization

    LANGUAGE_CODE = "en-us"
    TIME_ZONE = "UTC"
    USE_I18N = True
    USE_TZ = True

    Default Auto Field

    DEFAULT_AUTO_FIELD = "django.db.models.BigAutoField"

    REST Framework Configuration

    REST_FRAMEWORK = {
    'DEFAULT_AUTHENTICATION_CLASSES': (
    'rest_framework.authentication.TokenAuthentication',
    ),
    'DEFAULT_PARSER_CLASSES': (
    'rest_framework.parsers.JSONParser',
    'rest_framework.parsers.MultiPartParser',
    'rest_framework.parsers.FormParser',
    ),
    }

    Email Configuration

    EMAIL_BACKEND = 'django.core.mail.backends.smtp.EmailBackend'
    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')

    Custom User Model

    AUTH_USER_MODEL = 'users.User'

  • pat254
    pat254 Member Posts: 7

    This is the settings.py file in the deployed app

    import osfrom pathlib import Pathimport cloudinaryimport cloudinary.uploaderimport cloudinary.api

    Initialize Cloudinary

    cloudinary.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 Files

    MEDIA_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 Debug

    SECRET_KEY = os.getenv("DJANGO_SECRET_KEY", "your_default_secret_key")DEBUG = os.getenv("DJANGO_DEBUG", "False") == "True"ALLOWED_HOSTS = ['kabau.pythonanywhere.com']

    Installed Apps

    INSTALLED_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 Configuration

    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'CLOUDINARY_URL = f"cloudinary://{os.getenv('CLOUDINARY_API_KEY')}:{os.getenv('CLOUDINARY_API_SECRET')}@{os.getenv('CLOUDINARY_NAME')}"

    Middleware and Other Settings

    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 = TrueROOT_URLCONF = "webbackend.urls"WSGI_APPLICATION = "webbackend.wsgi.application"

    Database Configuration

    DATABASES = {'default': {'ENGINE': 'django.db.backends.mysql','NAME': 'xxxxxxx','USER': 'Kabau','PASSWORD': 'xxxxxx','HOST': 'xxxxx','PORT': '3306',}}

    Password Validators

    AUTH_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",},]

    Internationalization

    LANGUAGE_CODE = "en-us"TIME_ZONE = "UTC"USE_I18N = TrueUSE_TZ = True

    Default Auto Field

    DEFAULT_AUTO_FIELD = "django.db.models.BigAutoField"

    REST Framework Configuration

    REST_FRAMEWORK = {'DEFAULT_AUTHENTICATION_CLASSES': ('rest_framework.authentication.TokenAuthentication',),'DEFAULT_PARSER_CLASSES': ('rest_framework.parsers.JSONParser','rest_framework.parsers.MultiPartParser','rest_framework.parsers.FormParser',),}

    Email Configuration

    EMAIL_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 Model

    AUTH_USER_MODEL = 'users.User'

  • Cloudinary_John_H
    Cloudinary_John_H Cloudinary Staff Posts: 50

    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?