执行flask run时出现'NoneType' object has no attribute 'SSLContext'

题目描述

在Linux上部署Flask网站,用pipenv管理虚拟环境,python版本是3.6,执行flask run时出现AttributeError: 'NoneType' object has no attribute 'SSLContext'这个错误

题目来源及自己的思路

在网上查的时候看到SSLContext是在python2.9的版本引入的,linux服务器本身就自带python2.7版本,难道是执行flask run的时候使用的python2.7版本的吗?可是我的虚拟环境的版本是python3.6

相关代码

// 请把代码文本粘贴到下方(请勿用图片代替代码)
(Myambumy) root@iZ2zeejhgc3xqtkc4fw87rZ:~/Myambumy# flask run
Traceback (most recent call last):
File "/root/.local/share/virtualenvs/Myambumy-ulhRZoau/bin/flask", line 10, in <module>

sys.exit(main())

File "/root/.local/share/virtualenvs/Myambumy-ulhRZoau/lib/python3.6/site-packages/flask/cli.py", line 966, in main

cli.main(prog_name="python -m flask" if as_module else None)

File "/root/.local/share/virtualenvs/Myambumy-ulhRZoau/lib/python3.6/site-packages/flask/cli.py", line 586, in main

return super(FlaskGroup, self).main(*args, **kwargs)

File "/root/.local/share/virtualenvs/Myambumy-ulhRZoau/lib/python3.6/site-packages/click/core.py", line 717, in main

rv = self.invoke(ctx)

File "/root/.local/share/virtualenvs/Myambumy-ulhRZoau/lib/python3.6/site-packages/click/core.py", line 1135, in invoke

sub_ctx = cmd.make_context(cmd_name, args, parent=ctx)

File "/root/.local/share/virtualenvs/Myambumy-ulhRZoau/lib/python3.6/site-packages/click/core.py", line 641, in make_context

self.parse_args(ctx, args)

File "/root/.local/share/virtualenvs/Myambumy-ulhRZoau/lib/python3.6/site-packages/click/core.py", line 940, in parse_args

value, args = param.handle_parse_result(ctx, opts, args)

File "/root/.local/share/virtualenvs/Myambumy-ulhRZoau/lib/python3.6/site-packages/click/core.py", line 1477, in handle_parse_result

self.callback, ctx, self, value)

File "/root/.local/share/virtualenvs/Myambumy-ulhRZoau/lib/python3.6/site-packages/click/core.py", line 96, in invoke_param_callback

return callback(ctx, param, value)

File "/root/.local/share/virtualenvs/Myambumy-ulhRZoau/lib/python3.6/site-packages/flask/cli.py", line 742, in _validate_key

is_context = isinstance(cert, ssl.SSLContext)

AttributeError: 'NoneType' object has no attribute 'SSLContext'

你期待的结果是什么?实际看到的错误信息又是什么?

问题描述

问题出现的环境背景及自己尝试过哪些方法

相关代码

// 请把代码文本粘贴到下方(请勿用图片代替代码)
import os
import click
from flask import Flask,render_template
from Myambumy.settings import config
from Myambumy.extensions import db,mail,moment,bootstrap,login_manager,dropzone,csrf,avatars,whooshee
from Myambumy.blueprints.user import user_bp
from Myambumy.blueprints.main import main_bp
from Myambumy.blueprints.auth import auth_bp
from Myambumy.blueprints.ajax import ajax_bp
from Myambumy.blueprints.admin import admin_bp
from Myambumy.models import User,Role
from Myambumy.emails import send_confirm_email
from Myambumy.utils import generate_token,vaildate_token
from flask_wtf.csrf import CSRFError
from Myambumy.fakes import fake_admin,fake_comment,fake_photo,fake_tag,fake_user,fake_collect,fake_follow,fake_article
from Myambumy.models import Article,Photo,Tag,Collect,Collect_article,Notification
from flask_login import current_user

def create_app(config_name=None):

if config_name is None:
    config_name=os.getenv('FLASK_CONFIG','development')

app = Flask('Myambumy')
app.config.from_object(config[config_name])

register_extensions(app)
register_blueprints(app)
register_commands(app)
register_errorhandlers(app)
register_shell_context(app)
register_template_context(app)
return app

def register_extensions(app):

db.init_app(app)
bootstrap.init_app(app)
mail.init_app(app)
moment.init_app(app)
login_manager.init_app(app)
dropzone.init_app(app)
csrf.init_app(app)
avatars.init_app(app)
whooshee.init_app(app)

def register_blueprints(app):

app.register_blueprint(main_bp)
app.register_blueprint(user_bp, url_prefix='/user')
app.register_blueprint(auth_bp, url_prefix='/auth')
app.register_blueprint(ajax_bp,url_prefix='/ajax')
app.register_blueprint(admin_bp, url_prefix='/admin')

上下文

def register_shell_context(app):

@app.shell_context_processor
def make_shell_context():
    return dict(db=db,User=User,generate_token=generate_token,Article=Article,Tag=Tag,Photo=Photo,Collect=Collect,Collect_article=Collect_article,
                Notification=Notification)

def register_template_context(app):

@app.context_processor
def make_template_context():
    if current_user.is_authenticated:
        notification_count = Notification.query.with_parent(current_user).filter_by(is_read=False).count()
    else:
        notification_count = None
    return dict(notification_count=notification_count)

def register_errorhandlers(app):

@app.errorhandler(400)
def bad_request(e):
    return render_template('errors/400.html'),400

#文件大小错误处理
@app.errorhandler(403)
def forbidden(e):
    return render_template('errors/403.html'), 403

@app.errorhandler(404)
def page_not_found(e):
    return render_template('errors/404.html'), 404

@app.errorhandler(413)
def request_entity_too_large(e):
    return render_template('errors/413.html'), 413

@app.errorhandler(500)
def internal_server_error(e):
    return render_template('errors/500.html'), 500

@app.errorhandler(CSRFError)
def handle_csrf_error(e):
    return render_template('errors/400.html', description=e.description), 500

def register_commands(app):

@app.cli.command()
@click.option('--drop',is_flag=True,help='Create after drop.')
def initdb(drop):
    if drop:
        click.confirm('This operation will delete the database, do you want to continue?', abort=True)
        db.drop_all()
        click.echo('Drop tables.')
    db.create_all()
    click.echo('Initialized database.')

#初始化权限和角色
@app.cli.command()
def init():
    """Initialize Albumy."""
    click.echo('Initializing the database...')
    db.create_all()
    click.echo('Initializing the roles and permissions...')
    Role.init_role()

    click.echo('Done.')
    click.echo('Done.')

@app.cli.command()
@click.option('--user', default=10, help='Quantity of users, default is 10.')
@click.option('--photo', default=30, help='Quantity of photos, default is 500.')
@click.option('--tag', default=20, help='Quantity of tags, default is 500.')
@click.option('--collect', default=50, help='Quantity of collects, default is 500.')
@click.option('--comment', default=100, help='Quantity of comments, default is 500.')
@click.option('--follow', default=30, help='Quantity of follow, default is 50.')
@click.option('--article', default=30, help='Quantity of follow, default is 50.')
def forge(user, photo, tag, comment,collect,follow,article):
    """Generate fake data."""



    db.drop_all()
    db.create_all()

    click.echo('Initializing the roles and permissions...')
    Role.init_role()
    click.echo('Generating the administrator...')
    fake_admin()
    click.echo('Generating %d users...' % user)
    fake_user(user)
    click.echo('Generating %d tags...' % tag)
    fake_tag(tag)
    click.echo('Generating %d article...' % article)
    fake_article(article)
    click.echo('Generating %d photos...' % photo)
    fake_photo(photo)
    click.echo('Generating %d comments...' % comment)
    fake_comment(comment)
    click.echo('Generating %d collects...' % photo)
    fake_collect(collect)
    click.echo('Generating %d follows...' % follow)
    fake_follow(follow)

    click.echo('Done.')



你期待的结果是什么?实际看到的错误信息又是什么?

阅读 8.7k
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题