首页
学习
活动
专区
工具
TVP
发布
精选内容/技术社群/优惠产品,尽在小程序
立即前往

如何在Google App Engine中运行CherryPy Web服务器

在Google App Engine中运行CherryPy Web服务器需要遵循以下步骤:

  1. 安装CherryPy库:首先,确保已经安装了CherryPy库。如果没有,请使用以下命令安装:
代码语言:txt
复制
pip install cherrypy
  1. 创建CherryPy应用程序:接下来,创建一个CherryPy应用程序。以下是一个简单的示例:
代码语言:python
代码运行次数:0
复制
import cherrypy

class HelloWorld(object):
    def index(self):
        return "Hello World!"
    index.exposed = True

cherrypy.tree.mount(HelloWorld(), '/')
  1. 创建app.yaml文件:接下来,创建一个名为app.yaml的文件,以便为Google App Engine配置应用程序。以下是一个简单的示例:
代码语言:yaml
复制
runtime: python27
api_version: 1
threadsafe: true

handlers:
- url: /.*
  script: main.app
  1. 修改main.py文件:为了与Google App Engine兼容,需要对CherryPy应用程序进行一些修改。在main.py文件中,将以下代码添加到文件末尾:
代码语言:python
代码运行次数:0
复制
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()
  1. 部署应用程序:最后,部署应用程序到Google App Engine。可以使用Google Cloud SDK工具进行部署。在命令行中运行以下命令:
代码语言:txt
复制
gcloud app deploy

完成以上步骤后,CherryPy Web服务器应该已经在Google App Engine中运行。

页面内容是否对你有帮助?
有帮助
没帮助

相关·内容

领券