如何使用spyne将Pandas数据帧公开为SOAP服务?

2024-09-27 20:16:44 发布

您现在位置:Python中文网/ 问答频道 /正文

我想将带有out_protocol=XmlDocument()Pandas数据帧公开为SOAPweb服务。在

到目前为止,我只设法通过调用带有HTTP的web服务来公开String。这是工作代码。在

服务器代码:

from spyne import Application, srpc, ServiceBase, \
    Integer, Unicode, String

from spyne import Iterable
from spyne.protocol.http import HttpRpc
from spyne.protocol.soap import Soap11
from spyne.protocol.json import JsonDocument
from spyne.protocol.xml import XmlDocument

from spyne.server.wsgi import WsgiApplication

class HelloWorldService(ServiceBase):
    @srpc(String, Integer, _returns=String)
    def say_hello(name, times):
        s = ('Hi' + str(name)+' ')*times
        return s

application = Application([HelloWorldService],
    tns='spyne.examples.hello.http',
    in_protocol=HttpRpc(),    #Soap11 for SOAP client
    out_protocol=XmlDocument()
)
if __name__ == '__main__':
    from wsgiref.simple_server import make_server
    wsgi_app = WsgiApplication(application)
    server = make_server('127.0.0.1', 8000, wsgi_app)
    server.serve_forever()

客户代码:

^{pr2}$

我应该如何更改代码以最好地公开Pandas数据帧而不是字符串。以及如何让客户机使用SOAP协议发出请求?在

我对SOAP客户端的尝试:

from zeep import Client

client = Client('http://localhost:8000/?wsdl')
result = client.service.say_hello("Antonio", 10)

print(result)

web服务的预期输出应该是类似xml的表。下面是一个例子:

enter image description here


Tags: 代码namefromimportclienthttpwsgihello
1条回答
网友
1楼 · 发布于 2024-09-27 20:16:44

Soap服务在web服务中固有地使用xml。根据这个问题,我认为您需要xml来向服务器提供提要!!
正如您所说,您可以将result转换为pandas DF,然后从DF转换为xml,link

def to_xml(df, filename=None, mode='w'):
    def row_to_xml(row):
        xml = ['<item>']
        for i, col_name in enumerate(row.index):
            xml.append('  <field name="{0}">{1}</field>'.format(col_name, row.iloc[i]))
        xml.append('</item>')
        return '\n'.join(xml)
    res = '\n'.join(df.apply(row_to_xml, axis=1))

    if filename is None:
        return res
    with open(filename, mode) as f:
        f.write(res)

相关问题 更多 >

    热门问题