关于python:如何在Google App Engine(GAE)上使用bcrypt?

How to use bcrypt on Google App Engine (GAE)?

本问题已经有最佳答案,请猛点这里访问。

我发现了一个用于python的bcrypt库,它似乎非常易于使用:

  • bcrypt 1.0.1

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

1
2
3
4
5
6
7
8
9
10
>>> 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时,出现错误:

1
2
3
4
5
6
7
8
9
10
11
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文件以包含此库:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
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库?甚至有可能吗?


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

在这种情况下,最终结果应类似于:/ libs / bcrypt /

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

编辑:还请注意,您仅可以在您的App Engine项目上使用纯python代码。尝试py-bcrypt,对于App Engine上托管的项目,它的工作原理就像是一种魅力。