繁体   English   中英

Python模拟504网关超时错误

[英]Python Simulate 504 Gateway Timeout Error

我正在尝试模拟协议错误 504 网关超时。 这是我的服务器代码。 我想在 add() 方法中返回 504 错误。

from SimpleXMLRPCServer import SimpleXMLRPCServer

def add(x,y):
    return x+y

# A simple server with simple arithmetic functions
server = SimpleXMLRPCServer(("localhost", 8000))
print "Listening on port 8000..."
server.register_multicall_functions()
server.register_function(add, 'add')
server.serve_forever()

提前感谢您的时间。

以下是使用Flask模拟 504 错误的方法:

from flask import Flask, abort

app = Flask(__name__)


@app.route("/")
def fake_gateway_timeout_error():
    abort(504)


if __name__ == "__main__":
    app.run(port=8000, debug=True)

如果您使用浏览器尝试http://127.0.0.1:8000/ ,您将获得:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2 Final//EN">
<title>504 Gateway Timeout</title>
<h1>Gateway Timeout</h1>
<p>The connection to an upstream server timed out.</p>

退出状态 = 504。

当然,如果您想要不同的消息(文本而不是 HTML),您可以尝试:

@app.route("/")
def fake_gateway_timeout_error():
    return b'504 Gateway Timeout', 504

暂无
暂无

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

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