1

我正在尝试模拟协议错误 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()

提前感谢您的宝贵时间。

4

1 回答 1

1

以下是使用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
于 2017-01-10T20:52:55.373 回答