1

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.

aw88
  • 33
  • 1
  • 4

1 Answers1

1

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
Laurent LAPORTE
  • 21,958
  • 6
  • 58
  • 103
  • Hi, I tried your solution but I am getting a 405 error method not allowed, how to force 504 error – aw88 Jan 10 '17 at 21:32
  • By default it only accept GET method. You can change that in the `app.route` method/decorator. If I remember correctly, it has a `methods` parameter. Check the documentation. – Laurent LAPORTE Jan 10 '17 at 21:45
  • hey so it works ! I am using f = urllib2.urlopen("http://127.0.0.1:8000/").read() print f However, how do I access the message "504 Gateway Timeout". If I were to change it to "you have just encountered a 504 gateway timeout" – aw88 Jan 10 '17 at 21:48