【问题标题】:I'm using pyAMF in my app, but I want to set smart service like explained here:我在我的应用程序中使用 pyAMF,但我想设置智能服务,如下所述:
【发布时间】:2010-07-28 20:45:03
【问题描述】:

在正常的应用程序中我设置服务

services = {
    'users.login': login,
    'test': router
}

但我想这样做:

services = [
    'users.login',
    'test'
]

每个请求都进入路由器功能。这需要 2 个参数:服务名称(在这种情况下可以是“users.login”或“test”)和输入(即 flex 发送给 python 的对象) 那么如果来自 flex 的命令(服务)被命名为“users.login”。我想使用参数运行路由器,然后这将打开 commands.users.login 包中的登录功能。我该怎么做?谢谢。

【问题讨论】:

    标签: python pyamf


    【解决方案1】:

    如果我正确理解您的问题,为了实现这一点,您需要在您正在使用的 Gateway 类上覆盖 getServiceRequest

    from pyamf.remoting.gateway.django import DjangoGateway
    from pyamf.remoting.gateway import UnknownServiceError
    
    
    class MyGateway(DjangoGateway):
        def __init__(self, router_func, **kwargs):
            self.router = router_func
    
            DjangoGateway.__init__(self, **kwargs)
    
        def getServiceRequest(self, request, target):
            try:
                return DjangoGateway.getServiceRequest(self, request, target)
            except UnknownServiceError, e:
                pass
    
            # cached service was not found, try to discover it
            try:
                service_func = self.router(target)
            except:
                # perhaps some logging here
                service_func = None
    
            if not service_func:
                # couldn't find a service matching `target`, crap out appropriately
                raise e
    
            self.addService(service_func, target)
    
            return DjangoGateway.getServiceRequest(self, request, target)
    

    self.router 是您提供给网关构造函数的函数。它接受 AMF 远程处理请求的字符串目标并返回一个匹配函数。如果返回None或引发异常,则会向请求者返回未知的服务响应。

    希望这有助于为您的需求奠定基础。

    【讨论】:

    • 嘿!我正在使用谷歌,但我知道如何去做。但它没有按预期工作。它必须在运行时从包中导入函数。如果我请求“a.b.c.d”命令,我想从“app.controllers.a.b.c.d”包中加载“d”函数并运行它。这可能吗?
    【解决方案2】:

    昨晚解决了!

    from pyamf.remoting.gateway.google import WebAppGateway
    import logging
    
    class TottysGateway(WebAppGateway):
    def __init__(self, services_available, root_path, not_found_service, logger, debug):
        # override the contructor and then call the super
        self.services_available = services_available
        self.root_path = root_path
        self.not_found_service = not_found_service
        WebAppGateway.__init__(self, {}, logger=logging, debug=True)
    
    def getServiceRequest(self, request, target):
        # override the original getServiceRequest method
        try:
            # try looking for the service in the services list
            return WebAppGateway.getServiceRequest(self, request, target)
        except:
            pass
    
        try:
            # don't know what it does but is an error for now
            service_func = self.router(target)
        except:
            if(target in self.services_available):
                # only if is an available service import it's module
                # so it doesn't access services that should be hidden
                try:
                    module_path = self.root_path + '.' + target
                    paths = target.rsplit('.')
                    func_name = paths[len(paths) - 1]
                    import_as = '_'.join(paths) + '_' + func_name
                    import_string = "from "+module_path+" import "+func_name+' as service_func'
                    exec import_string
                except:
                    service_func = False
    
        if(not service_func):
            # if is not found load the default not found service
            module_path = self.rootPath + '.' + self.not_found_service
            import_string = "from "+module_path+" import "+func_name+' as service_func'
    
        # add the service loaded above
        assign_string = "self.addService(service_func, target)"
        exec assign_string
    
        return WebAppGateway.getServiceRequest(self, request, target)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-11-29
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多