本文介绍了Heroku上的Python bcrypt包给了AttributeError:'module'对象没有属性'ffi'的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在使用Heroku上的Flask应用程序时遇到了一个问题。当我部署到Heroku,并进入登录路线,我得到500内部服务器错误。它在本地正常工作。如何在Heroku上运行bcrypt包?

 应用程序中的错误:/ login [POST]上的异常
Traceback(最近一次调用最后一次):
在full_dispatch_request
中的/app/.heroku/python/lib/python2.7/site-packages/flask/app.py行1639行rv = self.dispatch_request()
文件/app/.heroku/python/lib/python2.7/site-packages/flask/app.py,第1625行,在dispatch_request
返回self.view_functions [ rule.endpoint](** req.view_args)
在包装器$ b $中的/app/.heroku/python/lib/python2.7/site-packages/flask_restful/__init__.py行477 b resp = resource(* args,** kwargs)
文件/app/.heroku/python/lib/python2.7/site-packages/flask/views.py,第84行,在
返回self.dispatch_request(* args,** kwargs)
文件/app/.heroku/python/lib/python2.7/site-packages/flask_restful/__init__.py,第587行,在dispatch_request
resp = meth(* args,** kwargs)
文件/app/app.py,行196,在
eli后f bcrypt.check_password_hash(user.password,password):
文件/app/.heroku/python/lib/python2.7/site-packages/flask_bcrypt.py,第193行,在check_password_hash
返回safe_str_cmp(bcrypt.hashpw(密码,pw_hash),pw_hash)
文件/app/.heroku/python/lib/python2.7/site-packages/bcrypt/__init__.py,第82行,在hashpw
hashed = _bcrypt.ffi.new(char [],128)
AttributeError:'module'object has no attribute'ffi'


解决方案

我遇到过类似的问题。这是我的堆栈跟踪的最后一部分的副本:

$ pre $ self_password = User.hashed_pa​​ssword $密码
在hashed_pa​​ssword
文件/app/application/models.py,第16行,在第163行的/app/.heroku/python/lib/python3.5/site-packages/flask_bcrypt.py in generate_password_hash
文件/app/.heroku/python/lib/python3.5/site-packages/bcrypt/__init__.py,第50行,在gensalt
output = _bcrypt.ffi.new( unsigned char [],30)
AttributeError:模块'bcrypt._bcrypt'没有属性'ffi'

我想知道这个问题是否是Heroku所特有的。我正在使用一些现有的Flask样板。但是这个Bcrypt的问题在以前的项目中也发生过,在Heroku上使用(不同的)样板文件Flask项目。

可能的解决方案1 ​​

玩弄不同的依赖组合。在一个案例中,当我在 requirements.txt 中包含 cryptography 时,问题就消失了。但正如让·席尔瓦(Jean Silva)在这个主题中所提到的那样,依赖可能是冲突的。所以,你可能想玩不同的组合,直到有用的东西。

可能的解决方案2

如果使用Flask,请尝试使用 werkzeug.security 程序包/模块来散列/检查散列,而不是使用 bcrypt 直接打包。在下面的示例中,在 models.py 中,注释掉这些行并添加新的行解决了这个问题。



<$从索引导入db,bcrypt
从导入数据库
从werkzeug.security导入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_pa​​ssword(密码)

@staticmethod
def hashed_pa​​ssword(密码):
#return bcrypt.generate_password_hash(密码)
返回generate_password_hash(密码)

@staticmethod
def get_user_with_email_and_password(email,password):
user = User.query.filter_by(email = email).first()
#如果user和bcrypt.check_password_hash(user.password,password):
如果用户和check_password_hash(user.password,password):
返回用户
else:
返回None


I'm having a problem using bcrypt with my Flask application on Heroku. When I deploy to Heroku and go to the login route I get 500 Internal server error. It works correctly locally. How do I get the bcrypt package working on Heroku?

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'
解决方案

I encountered a similar issue. Here is a copy of the last part of my stack trace:

  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' 

I'm wondering if this issue is particular to Heroku. I was using some existing Flask boilerplate. But this issue with Bcrypt has also happened to me in previous projects when using a (different) boilerplate Flask project on Heroku.

Possible Solution 1

Play around with different dependency combinations. In one case, the issue went away when I included cryptography in my requirements.txt. But as Jean Silva had mentioned in this thread, it is possible that dependencies could be in conflict. So you might want to play with different combinations until something works.

Possible Solution 2

If using Flask, try having the werkzeug.security package/module to hash / check hashes as opposed to using the bcrypt package directly. In example below in my models.py, commenting out such lines and adding new ones solved the issue for me.

# 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

这篇关于Heroku上的Python bcrypt包给了AttributeError:'module'对象没有属性'ffi'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-12 21:53