0
votes

Hi everebody!

Please help me, I'm stuck with cloudinary uploader error. The problem is that on localhost this function works well, but after deploying on pythonanywhere server it doesn't. Use:

    • Python3.4
      • sqlite database
        • SQL Alchemy 1.1.4
      • Flask 0.11.1
      • cloudinary python module
      • flask-googlemaps module
      • httplib2 module
      • requests module

Bits of my Code:

from flask import Flask, render_template, request, redirect, url_for, \
    flash, jsonify, make_response
from flask import session as login_session  # to avoid confusion with DB session
import random
import string
import httplib2
import json
import requests
import content
# Cloudinary API imports

import cloudinary
import cloudinary.uploader
from cloudinary.uploader import upload
import cloudinary.api
from cloudinary.utils import cloudinary_url


CLOUDINARY = {
  'cloud_name': 'ainsolence',
  'api_key': 'xxxxxxxxxxxxxxxxxxx',
  'api_secret': 'xxxxxxxxxxxxxxxxxxxxxxx',
}

cloudinary.config(cloud_name='ainsolence', api_key='xxxxxxxxxxxxxxxxxxxx',
                  api_secret='xxxxxxxxxxxxxxxxxxxxxxx')

...

# Create route for editCategory function

# Create route for uploadCategoryImage function

@app.route('/catalog/<int:category_id>/edit/upload/', methods=['GET',
           'POST'])
def uploadCategoryImage(category_id):
    if 'username' not in login_session or login_session['email'] \
        not in ADMIN:
        return redirect('/login')
    catalog = session.query(Category).all()
    categoryToEdit = \
        session.query(Category).filter_by(id=category_id).one()
    upload_result = None
    image_url = None
    if request.method == 'POST':
        file = request.files['file']
        if file:
            try:
                upload_result = upload(file, use_filename='true',
                                       folder='Menin_zherim/Categories')
                print ('This is upload result ' + upload_result)
                (image_url, options) = \
                    cloudinary_url(upload_result['public_id'], format='jpg')
                print ('Image url = ' + image_url)
                categoryToEdit.image_url = image_url
                session.add(categoryToEdit)
                session.commit()
                flash('Image for category successfully uploaded!')
                return redirect(url_for('editCategory',
                                category_id=category_id, catalog=catalog,
                                title='Edit category'))
            except:
                flash('Error in uploading', )
                return redirect(url_for('editCategory',
                                category_id=category_id, catalog=catalog,
                                title='Edit category'))
    else:
        return render_template('uploadCategoryImage.html',
                               category_id=category_id,
                               catalog=catalog,
                               category=categoryToEdit,
                               title='Upload image category')

#In uwsgi file I put this:
#Cloudinary base url
os.environ["CLOUDINARY_URL"] = "CLOUDINARY_URL=cloudinary://blablablabla:XXXXXXXXXXXXXXXXXXXX@ainsolence"

PS: of course I use real api_key and api_secret in my code

Error.log before I used try/except:

2016-12-20 07:37:01,224 :cloudinary.api.Error: Unexpected error - MaxRetryError("HTTPSConnectionPool(host='api.cloudinary.com', port=443): Max retries exceeded with url: /v1_1/ainsolence/image/upload (Caused by NewConnectionError('<urllib3.connection.VerifiedHTTPSConnection object at 0x7fe1621bbc88>: Failed to establish a new connection: [Errno 111] Connection refused',))",)
2016-12-20 07:37:01,200 :Exception on /catalog/4/edit/upload/ [POST]
Traceback (most recent call last):
  File "/usr/local/lib/python3.4/dist-packages/urllib3/connection.py", line 142, in _new_conn
    (self.host, self.port), self.timeout, **extra_kw)
  File "/usr/local/lib/python3.4/dist-packages/urllib3/util/connection.py", line 98, in create_connection
    raise err
  File "/usr/local/lib/python3.4/dist-packages/urllib3/util/connection.py", line 88, 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 "/usr/local/lib/python3.4/dist-packages/urllib3/connectionpool.py", line 595, in urlopen
    chunked=chunked)
  File "/usr/local/lib/python3.4/dist-packages/urllib3/connectionpool.py", line 352, in _make_request
    self._validate_conn(conn)
  File "/usr/local/lib/python3.4/dist-packages/urllib3/connectionpool.py", line 831, in _validate_conn
    conn.connect()
  File "/usr/local/lib/python3.4/dist-packages/urllib3/connection.py", line 254, in connect
    conn = self._new_conn()
  File "/usr/local/lib/python3.4/dist-packages/urllib3/connection.py", line 151, in _new_conn
    self, "Failed to establish a new connection: %s" % e)
urllib3.exceptions.NewConnectionError: <urllib3.connection.VerifiedHTTPSConnection object at 0x7fe1621bbc88>: Failed to establish a new connection: [Errno 111] Connection refused

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/home/Aintest/.local/lib/python3.4/site-packages/cloudinary/uploader.py", line 257, in call_api
    response = _http.request("POST", api_url, param_list, headers, **kw)
  File "/usr/local/lib/python3.4/dist-packages/urllib3/request.py", line 73, in request
    **urlopen_kw)
  File "/usr/local/lib/python3.4/dist-packages/urllib3/request.py", line 151, in request_encode_body
    return self.urlopen(method, url, **extra_kw)
  File "/usr/local/lib/python3.4/dist-packages/urllib3/poolmanager.py", line 248, in urlopen
    response = conn.urlopen(method, u.request_uri, **kw)
  File "/usr/local/lib/python3.4/dist-packages/urllib3/connectionpool.py", line 668, in urlopen
    release_conn=release_conn, **response_kw)
  File "/usr/local/lib/python3.4/dist-packages/urllib3/connectionpool.py", line 668, in urlopen
    release_conn=release_conn, **response_kw)
  File "/usr/local/lib/python3.4/dist-packages/urllib3/connectionpool.py", line 668, in urlopen
    release_conn=release_conn, **response_kw)
  File "/usr/local/lib/python3.4/dist-packages/urllib3/connectionpool.py", line 640, in urlopen
    _stacktrace=sys.exc_info()[2])
  File "/usr/local/lib/python3.4/dist-packages/urllib3/util/retry.py", line 287, in increment
    raise MaxRetryError(_pool, url, error or ResponseError(cause))
urllib3.exceptions.MaxRetryError: HTTPSConnectionPool(host='api.cloudinary.com', port=443): Max retries exceeded with url: /v1_1/ainsolence/image/upload (Caused by NewConnectionError('<urllib3.connection.VerifiedHTTPSConnection object at 0x7fe1621bbc88>: Failed to establish a new connection: [Errno 111] Connection refused',))

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/usr/local/lib/python3.4/dist-packages/flask/app.py", line 1988, in wsgi_app
    response = self.full_dispatch_request()
  File "/usr/local/lib/python3.4/dist-packages/flask/app.py", line 1641, in full_dispatch_request
    rv = self.handle_user_exception(e)
  File "/usr/local/lib/python3.4/dist-packages/flask/app.py", line 1544, in handle_user_exception
    reraise(exc_type, exc_value, tb)
  File "/usr/local/lib/python3.4/dist-packages/flask/_compat.py", line 33, in reraise
    raise value
  File "/usr/local/lib/python3.4/dist-packages/flask/app.py", line 1639, in full_dispatch_request
    rv = self.dispatch_request()
  File "/usr/local/lib/python3.4/dist-packages/flask/app.py", line 1625, in dispatch_request
    return self.view_functions[rule.endpoint](**req.view_args)
  File "/home/Aintest/menin-zherim/project.py", line 251, in uploadCategoryImage
    folder='Menin_zherim/Categories')
  File "/home/Aintest/.local/lib/python3.4/site-packages/cloudinary/uploader.py", line 36, in upload
    return call_api("upload", params, file=file, **options)
  File "/home/Aintest/.local/lib/python3.4/site-packages/cloudinary/uploader.py", line 259, in call_api
    raise Error("Unexpected error - {0!r}".format(e))

Error.log after:

    2016-12-20 08:40:04,836 :Starting new HTTPS connection (1): 

    api.cloudinary.com
2016-12-20 08:40:12,846 :Retrying (Retry(total=2, connect=None, read=None, redirect=None)) after connection broken by 'NewConnectionError('<urllib3.connection.VerifiedHTTPSConnection object at 0x7eff38c6dac8>: Failed to establish a new connection: [Errno 111] Connection refused',)': /v1_1/ainsolence/image/upload
2016-12-20 08:40:12,846 :Starting new HTTPS connection (2): api.cloudinary.com
2016-12-20 08:40:20,845 :Retrying (Retry(total=1, connect=None, read=None, redirect=None)) after connection broken by 'NewConnectionError('<urllib3.connection.VerifiedHTTPSConnection object at 0x7eff38c6dc88>: Failed to establish a new connection: [Errno 111] Connection refused',)': /v1_1/ainsolence/image/upload
2016-12-20 08:40:20,846 :Starting new HTTPS connection (3): api.cloudinary.com
2016-12-20 08:40:28,846 :Retrying (Retry(total=0, connect=None, read=None, redirect=None)) after connection broken by 'NewConnectionError('<urllib3.connection.VerifiedHTTPSConnection object at 0x7eff38c6dd68>: Failed to establish a new connection: [Errno 111] Connection refused',)': /v1_1/ainsolence/image/upload
2016-12-20 08:40:28,846 :Starting new HTTPS connection (4): api.cloudinary.com
2016-12-20 08:54:31,637 :Starting new HTTPS connection (5): api.cloudinary.com
2016-12-20 08:54:39,689 :Retrying (Retry(total=2, connect=None, read=None, redirect=None)) after connection broken by 'NewConnectionError('<urllib3.connection.VerifiedHTTPSConnection object at 0x7eff38c81c88>: Failed to establish a new connection: [Errno 111] Connection refused',)': /v1_1/ainsolence/image/upload
2016-12-20 08:54:39,690 :Starting new HTTPS connection (6): api.cloudinary.com
2016-12-20 08:54:47,689 :Retrying (Retry(total=1, connect=None, read=None, redirect=None)) after connection broken by 'NewConnectionError('<urllib3.connection.VerifiedHTTPSConnection object at 0x7eff38c81be0>: Failed to establish a new connection: [Errno 111] Connection refused',)': /v1_1/ainsolence/image/upload
2016-12-20 08:54:47,690 :Starting new HTTPS connection (7): api.cloudinary.com
2016-12-20 08:54:55,689 :Retrying (Retry(total=0, connect=None, read=None, redirect=None)) after connection broken by 'NewConnectionError('<urllib3.connection.VerifiedHTTPSConnection object at 0x7eff38c81d68>: Failed to establish a new connection: [Errno 111] Connection refused',)': /v1_1/ainsolence/image/upload
2016-12-20 08:54:55,690 :Starting new HTTPS connection (8): api.cloudinary.com

Please help me, if you have any advices where I can find the answer or just how I can understand this issue

Thank you Best Regards Anton

1

1 Answers

2
votes

If you're doing this from a free PythonAnywhere account, then you haven't configured the cloudinary library you're using to use the PythonAnywhere proxy for outgoing connections. The proxy details are proxy.server:3128.