How to use a different version of PyCrypto in GAE Python

僤鯓⒐⒋嵵緔 提交于 2019-12-25 03:26:11

问题


I downloaded the experimental version of PyCrypto (pycrypto-2.7a1.tar.gz). I have copied the "Crypto" directory (extracted from pycrypto-2.7a1.tar.gz) to my project folder.

In app.yaml file:

libraries:
- name: pycrypto
  version: 2.7 # latest 

I get error (at the time of deployment) if I try to give version as 2.7a1 or 2.7 for PyCrypto in app.yaml:

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.

How do I provide the correct PyCrypto version in app.yaml ?


回答1:


You use the app.yaml file to tell App Engine which libraries and versions you want to use only for those Third-party libraries available at the platform.

In your case, you want to use a version of the library that is not available, so you can't use that method to configure it.

Instead of that, you can upload to App Engine the libraries you want to use by following the method outlined in this other question:

  1. To download the library and unzipped inside your GAE application directory. In this example, the destination directory is called pycrypto26.
  2. To include the path to that library with something like
import sys
import os
sys.path.insert(0, os.path.join(os.path.dirname(__file__), 'pycrypto26/lib'))
  1. To import the relevant modules
import Crypto
from Crypto.Hash import SHA256, SHA512

A full working example is

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)


来源:https://stackoverflow.com/questions/25703151/how-to-use-a-different-version-of-pycrypto-in-gae-python

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!