sand
sand

Reputation: 183

Running Flask application with twisted

My server.py is as follows,

    from flask import Flask, jsonify, Response, redirect
    import json
    from UIAccess import UIAccess
    app=Flask(__name__)

    @app.route('/Hello/<username>')
    def id_no(username):
        id= obj.get_id(username)
        return json.dumps(id)

    if __name__ == '__main__':
        obj=UIAccess()
        app.run(threaded=True)

when I run the program and load the page using my browser I am able to view the output of 'id_no' but if I run the same program using twisted with the command,

    twistd web --wsgi server.app

I get an internal server error, I am wondering whether this is the correct way to do this?

Upvotes: 0

Views: 1508

Answers (1)

davidism
davidism

Reputation: 127360

You only create obj if __name__ == '__main__', which it does not when you run with something besides python server.py. But the id_no view depends on obj being defined, so it fails. Move obj = UIAccess() out of the guard block.

Upvotes: 1

Related Questions