繁体   English   中英

通过pyAMF频道发送的kwarg

[英]kwargs sent over pyAMF channel

我正在使用Cherrypy服务器通过pyAMF通道从python客户端接收请求。 我从下面的模拟开始,并且工作正常:

服务器:

import cherrypy
from pyamf.remoting.gateway.wsgi import WSGIGateway

def echo(*args, **kwargs):
    return (args, kwargs)

class Root(object):
    def index(self):
        return "running"
    index.exposed = True

services = {
   'myService.echo': echo,
}

gateway = WSGIGateway(services, debug=True)

cherrypy.tree.graft(gateway, "/gateway/")
cherrypy.quickstart(Root())

客户:

from pyamf.remoting.client import RemotingService

path = 'http://localhost:8080/gateway/'
gw = RemotingService(path)
service = gw.getService('myService')

print service.echo('one=1, two=3')

结果: [[u'one = 1,两个= 3'],{}]

现在,如果不是:

def echo(*args, **kwargs):
    return (args, kwargs)

我用:

def echo(**kwargs):
    return kwargs

并发送相同的请求,出现以下错误:

TypeError:echo()正好接受0个参数(给定1个)

同时:

>>> def f(**kwargs): return kwargs
... 
>>> f(one=1, two=3)
{'two': 3, 'one': 1}
>>> 

问题:为什么会这样? 请分享见解

我正在使用:python 2.5.2,cherrypy 3.1.2,pyamf 0.5.1

请注意,使用第一个echo函数,获得结果的唯一方法是按以下方式调用它:

echo(u"one=1, two=3")
# in words: one unicode string literal, as a positional arg

# *very* different from:
echo(one=1, two=3) # which seems to be what you expect

因此,您必须编写echo来接受位置参数或更改其调用方式。

缺省情况下,WSGIGateway设置expose_request=True ,这意味着WSGI环境dict被设置为该网关中任何服务方法的第一个参数。

这意味着回声应写为:

def echo(environ, *args):
    return args

PyAMF提供了一个装饰器,该装饰器允许您强制暴露请求,即使expose_request=False ,例如:

from pyamf.remoting.gateway import expose_request
from pyamf.remoting.gateway.wsgi import WSGIGateway

@expose_request
def some_service_method(request, *args):
    return ['some', 'thing']

services = {
    'a_service_method': some_service_method
}

gw = WSGIGateway(services, expose_request=False)

希望可以弄清楚为什么在这种情况下会出现TypeError

您正确地指出,您不能直接在PyAMF客户端/服务器调用中提供** kwarg,但是您可以使用默认的命名参数:

def update(obj, force=False):
    pass

然后,您可以访问该服务:

from pyamf.remoting.client import RemotingService

path = 'http://localhost:8080/gateway/'
gw = RemotingService(path)
service = gw.getService('myService')

print service.update('foo', True)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM