@app.route('/')
def hello():
    return 'Hello, world! running on %s' % request.host
if __name__ == '__main__':
    sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.bind(('localhost', 0))
port = sock.getsockname()[1]
sock.close()
app.run(port=port)
How to get the current port number in Flask?