Python Bottle:无法连接到服务器或对其进行调试

Python Bottle: Can't connect to server or debug it

当我尝试 运行 本示例中的服务器时,我可以看到它 运行 在正确的端口上。但是,无法从外部访问它(内部服务器错误),也无法通过常用方法在控制台中对其进行调试(看不到任何输出)。

我确定端口可用并且服务器 运行s。如何修复或调试这个?

from bottle import run, post, request, response, get, route

@route('/<path>', method = 'GET')
def process(path):
    response.content_type = 'text/html'
    return 'Hello World GET'

@route('/<path>', method = 'POST')
def process(path):
    response.content_type = 'text/html'
    return 'Hello World POST'

run(host='localhost', port=8000, debug=True)

@route 装饰器确实需要一个 path 变量来指定 URL 路径。

您的代码在 @route 装饰器中遗漏了 path 变量。

它应该像

那样工作
@route('/', method = 'GET')
def process(path):
    response.content_type = 'text/html'
    return 'Hello World POST'

原因是设置 host='localhost',这使得服务器无法从外部访问,我只能使用 localhost 进行访问。将声明更改为 host='0.0.0.0' 解决了我的问题。

如果您在使用 PyCharm 进行调试时看到这个,试试这个:

转到设置 --> 构建、执行、部署 -> Python 调试器。 在该对话框中,您会看到一个“Gevent 兼容”复选框。不知道它是如何在新项目中被取消的。

勾选该选项并享受!

source