烧瓶+;Pythonywhere上的Cloudinary上载错误 大家好

烧瓶+;Pythonywhere上的Cloudinary上载错误 大家好,python,web-services,flask,cloud,cloudinary,Python,Web Services,Flask,Cloud,Cloudinary,请帮帮我,我遇到了cloudinary上传错误。 问题是,在localhost上,这个函数工作得很好,但在pythonanywhere服务器上部署后就不行了。 使用: 蟒蛇3.4 sqlite数据库 SQL炼金术1.1.4 烧瓶0.11.1 cloudinary python模块 谷歌地图模块 httplib2模块 请求模块 我的代码: from flask import Flask, render_template, request, redirect, url_for, \

请帮帮我,我遇到了cloudinary上传错误。 问题是,在localhost上,这个函数工作得很好,但在pythonanywhere服务器上部署后就不行了。 使用:

    • 蟒蛇3.4
      • sqlite数据库
        • SQL炼金术1.1.4
      • 烧瓶0.11.1
      • cloudinary python模块
      • 谷歌地图模块
      • httplib2模块
      • 请求模块
我的代码:

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"
从flask导入flask,呈现模板,请求,重定向,url\
闪现,jsonify,做出回应
从flask导入会话作为登录会话,以避免与DB会话混淆
随机输入
导入字符串
导入httplib2
导入json
导入请求
导入内容
#Cloudinary API导入
云端输入
导入cloudinary.uploader
从cloudinary.uploader导入上载
导入cloudinary.api
从cloudinary.utils导入cloudinary\u url
云量={
“cloud_name”:“ainsolence”,
“api_密钥”:“XXXXXXXXXXXXXXXXX”,
“api_机密”:“XXXXXXXXXXXXXXXXXXXX”,
}
cloudinary.config(cloud\u name='ainsolence',api\u key='xxxxxxxxxxxxxxxxx',
api_secret='xxxxxxxxxxxxxxxxxxxxxxxx')
...
#为editCategory函数创建路由
#为uploadCategoryImage函数创建路由
@app.route('/catalog//edit/upload/',methods=['GET',
“发布”])
def上载类别图像(类别id):
如果“用户名”不在登录会话或登录会话[“电子邮件”]\
不在管理中:
返回重定向(“/login”)
catalog=session.query(Category).all()
类别编辑=\
session.query(Category).filter\u by(id=Category\u id).one()
上传结果=无
image\u url=None
如果request.method==“POST”:
file=request.files['file']
如果文件:
尝试:
上传\u结果=上传(文件,使用\u filename='true',
文件夹='Menin\u zherim/Categories')
打印('这是上载结果'+上载结果)
(图像url,选项)=\
cloudinary\u url(上传结果['public\u id',格式='jpg')
打印('图像url='+图像url)
categoryToEdit.image\u url=图像\u url
session.add(categoryToEdit)
session.commit()
flash('已成功上载类别的图像!')
返回重定向(url_用于('editCategory',
类别id=类别id,目录=目录,
title='Edit category'))
除:
flash('上传错误',)
返回重定向(url_用于('editCategory',
类别id=类别id,目录=目录,
title='Edit category'))
其他:
返回render_模板('uploadCategoryImage.html',
类别id=类别id,
目录=目录,
类别=类别编辑,
title='Upload image category')
#在uwsgi文件中,我放了以下内容:
#云数据库url
os.environ[“CLOUDINARY\u URL”]=“CLOUDINARY\u URL=cloudinary://blablablabla:XXXXXXXXXXXXXXXXXXXX@安索伦斯”
PS:当然,我在代码中使用了真正的api_密钥和api_密钥

使用try/except之前的Error.log:

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))
2016-12-20 07:37:01224:cloudinary.api.Error:意外错误-MaxRetryError(“HTTPSConnectionPool(host='api.cloudinary.com',port=443):url超过了最大重试次数:/v1\u 1/ainsolence/image/upload(由NewConnectionError引起(':未能建立新连接:[Errno 111]连接被拒绝',),),)
2016-12-20 07:37:01200:关于/catalog/4/edit/upload/[POST]的例外情况
回溯(最近一次呼叫最后一次):
文件“/usr/local/lib/python3.4/dist-packages/urllib3/connection.py”,第142行,位于康涅狄格州新城区
(self.host、self.port)、self.timeout、**额外功率)
文件“/usr/local/lib/python3.4/dist packages/urllib3/util/connection.py”,第98行,在create_connection中
提出错误
文件“/usr/local/lib/python3.4/dist packages/urllib3/util/connection.py”,第88行,在create_connection中
sock.connect(sa)
ConnectionRefusedError:[Errno 111]连接被拒绝
在处理上述异常期间,发生了另一个异常:
回溯(最近一次呼叫最后一次):
文件“/usr/local/lib/python3.4/dist-packages/urllib3/connectionpool.py”,urlopen中的第595行
分块=分块)
文件“/usr/local/lib/python3.4/dist packages/urllib3/connectionpool.py”,第352行,在请求中
自我验证连接(连接)
文件“/usr/local/lib/python3.4/dist-packages/urllib3/connectionpool.py”,第831行,位于康涅狄格州验证区
连接
文件“/usr/local/lib/python3.4/dist-packages/urllib3/connection.py”,第254行,在connect中
conn=自我。_new_conn()
文件“/usr/local/lib/python3.4/dist-packages/urllib3/connection.py”,第151行,位于康涅狄格州新城区
self,“无法建立新连接:%s”%e)
urllib3.exceptions.NewConnectionError::无法建立新连接:[Errno 111]连接被拒绝
在处理上述异常期间,发生了另一个异常:
回溯(最近一次呼叫最后一次):
call_api中的文件“/home/Aintest/.local/lib/python3.4/site packages/cloudinary/uploader.py”,第257行
响应=_http.request(“POST”、api_url、参数列表、标题,**kw)
文件“/usr/local/lib/python3.4/dist-packages/urllib3/request.py”,请求中的第73行
**urlopen(千瓦)
文件“/usr/local/lib/python3.4/dist packages/urllib3/request.py”,第151行,在request\u encode\u正文中
返回self.urlopen(方法,url,**额外\u kw)
文件“/usr/local/lib/python3.4/dist-packages/urllib3/poolmanager.py”,第248行,在urlopen中
response=conn.urlopen(方法,u.request\u uri,**kw)
urlopen中的文件“/usr/local/lib/python3.4/dist packages/urllib3/connectionpool.py”,第668行
释放连接=释放连接,**响应功率(kw)
    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