在Google App Engine中运行CherryPy Web服务器需要遵循以下步骤:
pip install cherrypy
import cherrypy
class HelloWorld(object):
def index(self):
return "Hello World!"
index.exposed = True
cherrypy.tree.mount(HelloWorld(), '/')
app.yaml
文件:接下来,创建一个名为app.yaml
的文件,以便为Google App Engine配置应用程序。以下是一个简单的示例:runtime: python27
api_version: 1
threadsafe: true
handlers:
- url: /.*
script: main.app
main.py
文件:为了与Google App Engine兼容,需要对CherryPy应用程序进行一些修改。在main.py
文件中,将以下代码添加到文件末尾:import os
import sys
from cherrypy import wsgiserver
from google.appengine.ext import webapp
def main():
# Get the current CherryPy application
app = cherrypy.tree.apps['/']
# Create a new WSGI application from the CherryPy application
wsgi_app = wsgiserver.WSGIPathInfoDispatcher({'/': app})
# Create a new webapp request handler that uses the WSGI application
class WSGIRequestHandler(webapp.RequestHandler):
def __init__(self, *args, **kwargs):
webapp.RequestHandler.__init__(self, *args, **kwargs)
def handle_request(self):
env = self.request.environ
env['SCRIPT_NAME'] = ''
wsgi_app(env, self.response.out)
# Start the webapp application
webapp.WSGIApplication([('/.*', WSGIRequestHandler)], debug=True)
if __name__ == '__main__':
main()
gcloud app deploy
完成以上步骤后,CherryPy Web服务器应该已经在Google App Engine中运行。
领取专属 10元无门槛券
手把手带您无忧上云