Python Simulate 504 Gateway Timeout Error

3.6k views Asked by At

I am trying to simulate a protocol error 504 gateway timeout. This is my server code. I would like to return a 504 error in the add() method.

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.

1

There are 1 answers

3
Laurent LAPORTE On BEST ANSWER

Here is how you can simulate a 504 error with Flask:

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:

<!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.

Of course, if you want a different message (text instead of HTML), you can try:

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