我在我的应用程序中使用 pyAMF,但我想设置智能服务,如下所述:

发布于 2024-09-11 16:20:41 字数 381 浏览 4 评论 0原文

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

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

,但我想这样做:

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

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

In a normal application i set services

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

but I would like to do like this:

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

and every request goes to router function. this takes 2 params: service name (can be "users.login" or "test" in this case) and input (that is the object sent by flex to python)
then if the command(service) from flex is named "users.login". I would like to run the router with the params and then this will open the function login in the commands.users.login package. How would I do that? Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

错爱 2024-09-18 16:20:41

如果我正确理解你的问题,为了实现这一点,你将需要重写你正在使用的网关类上的 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 或引发异常,则会向请求者返回未知的服务响应。

希望这能在一定程度上为您的需求奠定基础。

If I understand your question correctly, in order to achieve this, you are going to need to to override getServiceRequest on the Gateway class that you are using:

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 is a function that you supply to the constructor of the gateway. It takes the string target of the AMF remoting request and returns a matching function. If it returns None or raises an exception, an unknown service response will be returned to the requestor.

Hopefully this goes some way to laying the groundwork for what you require.

说好的呢 2024-09-18 16:20:41

昨晚解决了!

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)

Solved last night!

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)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文