简体   繁体   English

在Cherrypy中多次调用同一终结点

[英]Calling the same endpoint more than once in Cherrypy

I have created a simple web server with Cherrypy and my resources are only available the first time I make the call. 我使用Cherrypy创建了一个简单的Web服务器,并且只有在我第一次拨打电话时才能使用我的资源。

For example: 例如:

When I put the following on the browser http://127.0.0.1:8080/catalog/about - it displays the Cherrypy version as {"version": "18.1.1"} - which is correct. 当我在浏览器http://127.0.0.1:8080/catalog/about上放置以下内容时-它将Cherrypy版本显示为{"version": "18.1.1"} -是正确的。

However, If I hit enter one more time I get the following " 404 Not Found " 但是,如果我再按一次Enter键,则会显示以下“ 404 Not Found

Is this a safety feature and how do I change this settings? 这是一项安全功能,如何更改此设置? I don't understand 我不明白

server.py: server.py:

import os
import os.path
import random
import string
import json
import cherrypy
from controller import Catalog

class Server(object):

  @cherrypy.expose
  def index(self):
    return open('../cococlient/build/index.html')

def cors():
  cherrypy.response.headers["Access-Control-Allow-Origin"] = "*"

if __name__ == '__main__':
    conf = {
        '/': {
            'tools.staticdir.root': os.path.abspath(os.getcwd())
        },
        '/catalog': {
            'tools.CORS.on': True,
            'tools.response_headers.on': True
        },
        '/static': {
            'tools.staticdir.on': True,
            'tools.staticdir.dir': '../cococlient/build/static'
        }
    }

    server = Server()
    server.catalog = Catalog()
    cherrypy.tools.CORS = cherrypy.Tool('before_handler', cors)

    # cherrypy.tree.mount(server, '/', conf)
    # cherrypy.engine.start()
    # cherrypy.engine.block()

    cherrypy.quickstart(server, '/', conf)

controller.py: controller.py:

import cherrypy

class Catalog(object):

  @cherrypy.expose
  @cherrypy.tools.json_out()
  def about(self):
    self.about = {
        "version": cherrypy.__version__
    }
    return self.about

Found the issue - the problem is that I am using the same name for the method and variable inside. 找到了问题-问题是我在内部使用相同的名称作为方法和变量。 I am used to Java where I have no such issues. 我已经习惯了Java,而我却没有这类问题。

So, instead of 所以,代替

  @cherrypy.expose
  @cherrypy.tools.json_out()
  def about(self):
    self.about = {
        "version": cherrypy.__version__
    }
    return self.about

change to 改成

  @cherrypy.expose
  @cherrypy.tools.json_out()
  def about(self):
    self.someOtherName = {
        "version": cherrypy.__version__
    }
    return self.someOtherName

Success! 成功!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM