1

在一个普通的应用程序中,我设置了服务

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

但我想这样做:

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

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

4

2 回答 2

2

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

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或引发异常,则会将未知的服务响应返回给请求者。

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

于 2010-08-13T00:28:11.660 回答
0

昨晚解决了!

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)
于 2010-08-21T16:33:05.500 回答