python exec函数在spyne模块中不起作用

时间:2018-08-02 00:34:28

标签: python-3.x spyne

#!/usr/bin/env`` python
# encoding: utf8

import logging
logging.basicConfig(level=logging.DEBUG)
from spyne.decorator import srpc
from spyne import Application, rpc, ServiceBase, \
    Integer, Unicode
from spyne import Iterable
from spyne.protocol.http import HttpRpc
from spyne.protocol.json import JsonDocument
from spyne.server.wsgi import WsgiApplication


class Test(ServiceBase):
    @rpc(Integer, _returns=Iterable(Unicode))
    def say_hello(self, times):
        for i in range(times):
            yield "say hello."
            print(i)

    @rpc(Unicode, _returns=Unicode)
    def execcodes(ctx, codes):
        exec(codes)
        print('|'.join(locals())
        print('|'.join(globals())

if __name__ == '__main__':
    from wsgiref.simple_server import make_server
    logging.basicConfig(level=logging.DEBUG)

    application = Application([Test], 'spyne.examples.hello.http',

                            in_protocol=HttpRpc(validator='soft'),

                            out_protocol=JsonDocument(ignore_wrappers=True),
                            )

    wsgi_application = WsgiApplication(application)

    server = make_server('0.0.0.0', 22346, wsgi_application)

    logging.info("listening to http://127.0.0.1:22346")
    logging.info("wsdl is at: http://localhost:22346/?wsdl")

    server.serve_forever()

我试图建立一个Web服务,用户通过该服务提供一块python代码,然后该服务反馈一些信息,但是python函数exec似乎在spyne中不起作用。 我通过127.0.0.1:22346/Test?execcodes='a=1\nb=2\nprint(a+b)\n'触发了函数execcodes,关于exec的错误没有出现,但是在日志记录中没有打印,并且我在本地变量和全局变量中都找不到a和b,所以我很困惑我的代码是否已执行?如果执行了,我在哪里可以找到变量?

0 个答案:

没有答案