Absolutely that's what I'm looking for! Thanks a lot!

However, I'm wondering if Twisted and Klein support filtering results. For 
example, the User table in MySQL contains id, name and age fields. How can I 
get all users whose name are "Alan" and age > 16? It seems I need a filter, but 
I'm not sure how to implement such a filter or resource with Twisted and Klein.






在 2015-11-06 06:02:53,"Glyph Lefkowitz" <gl...@twistedmatrix.com> 写道:


On Nov 5, 2015, at 1:28 AM, Wang Yan <snailco...@163.com> wrote:


Thank you to remind me of Klein. I'm very new to Twisted and Klein. What I've 
known is that Klein is a little like Flask, with which I can implement a 
UserAPI as follows:
   
    from flask import Flask
    from flask.ext.restful import Api, Resource

    api = Flask(__name__)
    api = Api(app)

    class UserAPI(Resource):
        def get(self, user_id):
            pass

        def post(self, user_id):
            pass

    api.add_resource(UserAPI, '/users/<int:id>', endpoint='user')

Is there any similar usage in Klein?



Yes; in fact this extension for Flask is similar to how twisted.web works 
internally.  I think you're looking for something like this:


from klein import run, route


from twisted.web.resource import Resource


class User(Resource, object):
    def __init__(self, user_id):
        super(User, self).__init__()
        self.user_id = user_id
    def render_GET(self, request):
        pass
    def render_POST(self, request):
        pass


@route("/users/<int:user_id>")
def user(request, user_id):
    return User(user_id)


run("localhost", 8080)
_______________________________________________
Twisted-Python mailing list
Twisted-Python@twistedmatrix.com
http://twistedmatrix.com/cgi-bin/mailman/listinfo/twisted-python

Reply via email to