Jea*_*lva 5 python heroku bcrypt flask
我在Heroku上使用我的Flask应用程序使用bcrypt时遇到问题.当我部署到Heroku并转到登录路由时,我得到500内部服务器错误.它在本地正常工作.如何在Heroku上运行bcrypt包?
ERROR in app: Exception on /login [POST]
Traceback (most recent call last):
File "/app/.heroku/python/lib/python2.7/site-packages/flask/app.py", line 1639, in full_dispatch_request
rv = self.dispatch_request()
File "/app/.heroku/python/lib/python2.7/site-packages/flask/app.py", line 1625, in dispatch_request
return self.view_functions[rule.endpoint](**req.view_args)
File "/app/.heroku/python/lib/python2.7/site-packages/flask_restful/__init__.py", line 477, in wrapper
resp = resource(*args, **kwargs)
File "/app/.heroku/python/lib/python2.7/site-packages/flask/views.py", line 84, in view
return self.dispatch_request(*args, **kwargs)
File "/app/.heroku/python/lib/python2.7/site-packages/flask_restful/__init__.py", line 587, in dispatch_request
resp = meth(*args, **kwargs)
File "/app/app.py", line 196, in post
elif bcrypt.check_password_hash(user.password, password):
File "/app/.heroku/python/lib/python2.7/site-packages/flask_bcrypt.py", line 193, in check_password_hash
return safe_str_cmp(bcrypt.hashpw(password, pw_hash), pw_hash)
File "/app/.heroku/python/lib/python2.7/site-packages/bcrypt/__init__.py", line 82, in hashpw
hashed = _bcrypt.ffi.new("char[]", 128)
AttributeError: 'module' object has no attribute 'ffi'
Run Code Online (Sandbox Code Playgroud)
我遇到了类似的问题.这是我的堆栈跟踪的最后一部分的副本:
self.password = User.hashed_password(password)
File "/app/application/models.py", line 16, in hashed_password
File "/app/.heroku/python/lib/python3.5/site-packages/flask_bcrypt.py", line 163, in generate_password_hash
File "/app/.heroku/python/lib/python3.5/site-packages/bcrypt/__init__.py", line 50, in gensalt
output = _bcrypt.ffi.new("unsigned char[]", 30)
AttributeError: module 'bcrypt._bcrypt' has no attribute 'ffi'
Run Code Online (Sandbox Code Playgroud)
我想知道这个问题是否特别适用于Heroku.我正在使用一些现有的Flask样板.但是,在Heroku上使用(不同的)样板Flask项目时,Bcrypt的这个问题也发生在我以前的项目中.
可能的解决方案1
玩弄不同的依赖组合.在一个案例中,当我加入cryptography我的问题时,问题就消失了requirements.txt.但正如让席尔瓦在这个帖子中提到的那样,依赖关系可能会发生冲突.所以你可能想要使用不同的组合,直到有效.
可能的解决方案2
如果使用Flask,请尝试使用werkzeug.security包/模块来散列/检查哈希值,而不是bcrypt直接使用包.在我的下面的示例中models.py,注释掉这些行并添加新行解决了我的问题.
# from index import db, bcrypt
from index import db
from werkzeug.security import generate_password_hash, check_password_hash
class User(db.Model):
id = db.Column(db.Integer(), primary_key=True)
email = db.Column(db.String(255), unique=True)
password = db.Column(db.String(255))
def __init__(self, email, password):
self.email = email
self.active = True
self.password = User.hashed_password(password)
@staticmethod
def hashed_password(password):
# return bcrypt.generate_password_hash(password)
return generate_password_hash(password)
@staticmethod
def get_user_with_email_and_password(email, password):
user = User.query.filter_by(email=email).first()
# if user and bcrypt.check_password_hash(user.password, password):
if user and check_password_hash(user.password, password):
return user
else:
return None
Run Code Online (Sandbox Code Playgroud)
我找到了解决方案,我使用了以下软件包:bcrypt、flask_bcrypt和py-crypt。所以我卸载了py-bcrypt,可能这个包与包冲突bcrypt。
pip uninstall py-bcrypt
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2357 次 |
| 最近记录: |