【问题标题】:How to use bcrypt on Google App Engine (GAE)? [duplicate]如何在 Google App Engine (GAE) 上使用 bcrypt? [复制]
【发布时间】:2014-06-22 07:58:44
【问题描述】:

我找到了一个似乎很容易使用的python bcrypt库:

在我的本地机器上安装并测试 hello world 示例后一切似乎都很好:

>>> import bcrypt
>>> password = b"super secret password"
>>> # Hash a password for the first time, with a certain number of rounds
>>> hashed = bcrypt.hashpw(password, bcrypt.gensalt(10))
>>> # Check that a unhashed password matches one that has previously been
>>> #   hashed
>>> if bcrypt.hashpw(password, hashed) == hashed:
...     print("It Matches!")
... else:
...     print("It Does not Match :(")

但是,在我的 GAE 应用程序中,当我使用 import bcrypt 时出现错误:

Traceback (most recent call last):
  File "/home/pedro/google_appengine/google/appengine/runtime/wsgi.py", line 239, in Handle
    handler = _config_handle.add_wsgi_middleware(self._LoadHandler())
  File "/home/pedro/google_appengine/google/appengine/runtime/wsgi.py", line 298, in _LoadHandler
    handler, path, err = LoadObject(self._handler)
  File "/home/pedro/google_appengine/google/appengine/runtime/wsgi.py", line 84, in LoadObject
    obj = __import__(path[0])
  File "/home/pedro/google_appengine/hw4/blog.py", line 8, in <module>
    import bcrypt
ImportError: No module named bcrypt
INFO     2014-05-05 21:17:04,375 module.py:639] default: "GET /blog/signup HTTP/1.1" 500 -

这让我相信我必须更改 app.yaml 文件以包含此库:

application: calm-grid-571
version: 1
runtime: python27
api_version: 1
threadsafe: False

handlers:
- url: /static
  static_dir: static

- url: /.*
  script: blog.app

libraries:
- name: jinja2
  version: latest

- name: PIL
  version: latest

但是,在查看受支持库的官方页面时,我找不到任何关于 bcrypt 的信息。

那么,如何在 GAE 中使用 bcrypt 库?有没有可能?

【问题讨论】:

  • 将源代码放在您的文件夹中是一种方法。

标签: python google-app-engine password-protection bcrypt


【解决方案1】:

您必须将 bcrypt(或任何其他非嵌入式库)的源代码包含到您的项目中。 建议在项目的根目录(与 app.yaml 所在的同一级别)创建一个 libs 文件夹,并根据需要放置尽可能多的库源代码。

对于这种情况,最终结果应如下所示:/libs/bcrypt/

确保在您希望代码将此文件夹视为一个包的任何新文件夹中包含 __init__.py 空白文件。之后,只需导入模块:from libs.bcrypt import bcrypt

编辑:还请注意,您的应用引擎项目中只能有 pure python code。试试py-bcrypt,它对于托管在 App Engine 上的项目来说就像一个魅力。

【讨论】:

  • 谢谢,使用 py-crypt 真的很有帮助!
猜你喜欢
  • 1970-01-01
  • 2012-09-11
  • 2011-03-11
  • 2013-03-31
  • 2013-07-15
  • 2017-02-26
  • 2011-04-08
  • 1970-01-01
  • 2017-11-14
相关资源
最近更新 更多