【问题标题】:How to use a different version of PyCrypto in GAE Python如何在 GAE Python 中使用不同版本的 PyCrypto
【发布时间】:2014-09-06 17:54:58
【问题描述】:

我下载了 PyCrypto 的实验版 (pycrypto-2.7a1.tar.gz)。我已将“加密”目录(从 pycrypto-2.7a1.tar.gz 中提取)复制到我的项目文件夹中。

在 app.yaml 文件中:

libraries:
- name: pycrypto
  version: 2.7 # latest 

如果我尝试在 app.yaml 中为 PyCrypto 提供 2.7a1 或 2.7 版本,则会出现错误(在部署时):

appcfg.py: error: Error parsing C:\gaurav\coding\python\x\x\app.yaml: pycrypto version "2.7" is not supported, use one of: "2.3", "2.6" or "latest" ("latest" recommended for development only)
  in "C:\gaurav\coding\python\x\x\app.yaml", line 73, column 1.

如何在 app.yaml 中提供正确的 PyCrypto 版本?

【问题讨论】:

    标签: google-app-engine python-2.7 pycrypto app.yaml


    【解决方案1】:

    您使用app.yaml 文件告诉App Engine 您希望为那些Third-party libraries available at the platform 使用哪些库和版本。

    在你的情况下,你想使用一个不可用的库版本,所以你不能使用那个方法来配置它。

    除此之外,您可以按照this other question 中概述的方法将要使用的库上传到 App Engine:

    1. 下载库并解压到您的 GAE 应用程序目录中。在此示例中,目标目录称为 pycrypto26
    2. 使用类似
    3. 的内容包含该库的路径
    import sys
    import os
    sys.path.insert(0, os.path.join(os.path.dirname(__file__), 'pycrypto26/lib'))
    
    1. 导入相关模块
    import Crypto
    from Crypto.Hash import SHA256, SHA512
    

    一个完整的工作示例是

    import webapp2
    import logging
    
    import sys
    import os
    sys.path.insert(0, os.path.join(os.path.dirname(__file__), 'pycrypto26/lib'))
    
    import Crypto
    from Crypto.Hash import SHA256, SHA512
    
    class MainPage(webapp2.RequestHandler):
        def get(self):
            logging.info("Running PyCrypto with version %s" % Crypto.__version__)
            self.response.write('<html><body>')
            self.response.write( SHA256.new('abcd').hexdigest() + "<br>" )
            self.response.write( SHA512.new('abcd').hexdigest() + "<br>")
            self.response.write('</body></html>')
    
    application = webapp2.WSGIApplication([
        ('/', MainPage),
    ], debug=True)
    

    【讨论】:

    • 谢谢马里奥。我曾尝试过类似的方法,但由于其他一些内部库的调用,这种方法当时没有奏效。 Google 最终升级到了我需要的实验版本,这解决了我的问题。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-08-01
    • 1970-01-01
    • 1970-01-01
    • 2013-12-11
    相关资源
    最近更新 更多