简体   繁体   English

Python模拟504网关超时错误

[英]Python Simulate 504 Gateway Timeout Error

I am trying to simulate a protocol error 504 gateway timeout.我正在尝试模拟协议错误 504 网关超时。 This is my server code.这是我的服务器代码。 I would like to return a 504 error in the add() method.我想在 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()

Thank you in advance for your time.提前感谢您的时间。

Here is how you can simulate a 504 error with Flask :以下是使用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)

If you try http://127.0.0.1:8000/ with your browser, you'll get:如果您使用浏览器尝试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>

With the exit status = 504.退出状态 = 504。

Of course, if you want a different message (text instead of HTML), you can try:当然,如果您想要不同的消息(文本而不是 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