Python 基于WSGI的Cherrypy路由

Python 基于WSGI的Cherrypy路由,python,cherrypy,python-routes,Python,Cherrypy,Python Routes,我试图将某些URL路由到移植的WSGI应用程序,并将子URL路由到普通的cherrypy页面处理程序 我需要以下路线来工作。所有其他路线应返回404 /api->WSGI /api?wsdl->WSGI /api/goodurl->页面处理程序 /api/badurl->404错误 安装在/api上的WSGI应用程序是一个基于SOAP的遗留应用程序。它需要接受?wsdl参数,但仅此而已 我正在/api/some\u资源中编写一个新的RESTful api 我遇到的问题是,如果资源不存在,它最

我试图将某些URL路由到移植的WSGI应用程序,并将子URL路由到普通的cherrypy页面处理程序

我需要以下路线来工作。所有其他路线应返回404

  • /api->WSGI
  • /api?wsdl->WSGI
  • /api/goodurl->页面处理程序
  • /api/badurl->404错误
安装在/api上的WSGI应用程序是一个基于SOAP的遗留应用程序。它需要接受?wsdl参数,但仅此而已

我正在/api/some\u资源中编写一个新的RESTful api

我遇到的问题是,如果资源不存在,它最终会向遗留soap应用程序发送错误的请求。最后一个示例“/api/badurl”最终进入WSGI应用程序

有没有办法告诉cherrypy只发送前两条路由到WSGI应用程序?

我就我的问题写了一个简单的例子:

import cherrypy

globalConf = {
    'server.socket_host': '0.0.0.0',
    'server.socket_port': 8080,
}
cherrypy.config.update(globalConf)

class HelloApiWsgi(object):
    def __call__(self, environ, start_response):
        start_response('200 OK', [('Content-Type', 'text/html')])
        return ['Hello World from WSGI']

class HelloApi(object):
    @cherrypy.expose
    def index(self):
        return "Hello from api"

cherrypy.tree.graft(HelloApiWsgi(), '/api')
cherrypy.tree.mount(HelloApi(), '/api/hello')

cherrypy.engine.start()
cherrypy.engine.block()
下面是一些单元测试:

import unittest
import requests

server = 'localhost:8080'

class TestRestApi(unittest.TestCase):

    def testWsgi(self):
        r = requests.get('http://%s/api?wsdl'%(server))
        self.assertEqual(r.status_code, 200)
        self.assertEqual(r.text, 'Hello World from WSGI')

        r = requests.get('http://%s/api'%(server))
        self.assertEqual(r.status_code, 200)
        self.assertEqual(r.text, 'Hello World from WSGI')

    def testGoodUrl(self):
        r = requests.get('http://%s/api/hello'%(server))
        self.assertEqual(r.status_code, 200)
        self.assertEqual(r.text, 'Hello from api')

    def testBadUrl(self):
        r = requests.get('http://%s/api/badurl'%(server))
        self.assertEqual(r.status_code, 404)
产出:

nosetests test_rest_api.py
F..
======================================================================
FAIL: testBadUrl (webserver.test_rest_api.TestRestApi)
----------------------------------------------------------------------
Traceback (most recent call last):
  File line 25, in testBadUrl
    self.assertEqual(r.status_code, 404)
AssertionError: 200 != 404
-------------------- >> begin captured stdout << ---------------------
Hello World from WSGI
nosetests test\u rest\u api.py
F
======================================================================
失败:testBadUrl(webserver.test\u rest\u api.TestRestApi)
----------------------------------------------------------------------
回溯(最近一次呼叫最后一次):
testBadUrl中的文件第25行
self.assertEqual(r.status\U代码,404)
断言错误:200!=404

-------------------->>begin captured stdout序言:我不得不提到,我希望每个人都能以如此完整的形式提出问题,并通过各种方式验证答案:-)

CherryPy范围之外的解决方案:

  • 在前端服务器上进行URL预处理,例如nginx
  • 创建自己的,即将您的传统WSGI应用程序包装到另一个将过滤URL的应用程序中
后者可能是最好的方法,但这里是CherryPy的方法。文件部分说:

您不能将工具与外部WSGI应用程序一起使用

此外,您不能设置自定义调度程序。但是您可以对应用程序树进行子类化

#!/usr/bin/env python


import cherrypy


class Tree(cherrypy._cptree.Tree):

  def __call__(self, environ, start_response):
    # do more complex check likewise
    if environ['PATH_INFO'].startswith('/api/badurl'):
      start_response('404 Not Found', [])
      return []

    return super(Tree, self).__call__(environ, start_response)

cherrypy.tree = Tree()


globalConf = {
  'server.socket_host': '0.0.0.0',
  'server.socket_port': 8080,
}
cherrypy.config.update(globalConf)


class HelloApiWsgi:

  def __call__(self, environ, start_response):
    start_response('200 OK', [('Content-Type', 'text/html')])
    return ['Hello World from WSGI']

class HelloApi:

  @cherrypy.expose
  def index(self):
    return "Hello from api"


cherrypy.tree.graft(HelloApiWsgi(), '/api')
cherrypy.tree.mount(HelloApi(), '/api/hello')


if __name__ == '__main__':
  cherrypy.engine.signals.subscribe()
  cherrypy.engine.start()
  cherrypy.engine.block()

非常感谢saaj!