python开发webservice,SAP端调用
程序员文章站
2022-04-28 14:46:45
...
1、使用python开发一个webservice服务端的接口,并暴露出去
python环境:3.6
IDE:Eclipse Java EE IDE for Web Developers.
Version: Oxygen.3a Release (4.7.3a)
anaconda
需要注意的是:py3和py2的soap的支持包不太一样,py2使用的是BaseHTTPServer,而py3使用的是spyne
2、python webservice服务器端代码
'''
Created on 2018年7月16日
@author: lin
'''
# Application is the glue between one or more service definitions, interface and protocol choices.
from spyne import Application
# @rpc decorator exposes methods as remote procedure calls
# and declares the data types it accepts and returns
from spyne import rpc
# spyne.service.ServiceBase is the base class for all service definitions.
from spyne import ServiceBase
# The names of the needed types for implementing this service should be self-explanatory.
from spyne import Iterable, Integer, Unicode
from spyne.protocol.soap import Soap11
# Our server is going to use HTTP as transport, It’s going to wrap the Application instance.
from spyne.server.wsgi import WsgiApplication
# step1: Defining a Spyne Service
class HelloWorldService(ServiceBase):
@rpc(Unicode, Integer, _returns=Iterable(Unicode))
def say_hello(self, name, times):
"""Docstrings for service methods appear as documentation in the wsdl.
<b>What fun!</b>
@param name: the name to say hello to
@param times: the number of times to say hello
@return When returning an iterable, you can use any type of python iterable. Here, we chose to use generators.
"""
for i in range(times):
yield u'Hello, %s' % name
# step2: Glue the service definition, input and output protocols
soap_app = Application([HelloWorldService], 'spyne.examples.hello.soap',
in_protocol=Soap11(validator='lxml'),
out_protocol=Soap11())
# step3: Wrap the Spyne application with its wsgi wrapper
wsgi_app = WsgiApplication(soap_app)
if __name__ == '__main__':
import logging
from wsgiref.simple_server import make_server
# configure the python logger to show debugging output
logging.basicConfig(level=logging.DEBUG)
logging.getLogger('spyne.protocol.xml').setLevel(logging.DEBUG)
logging.info("listening to http://saplh:8000")
logging.info("wsdl is at: http://saplh:8000/?wsdl")
# step4:Deploying the service using Soap via Wsgi
# register the WSGI application as the handler to the wsgi server, and run the http server
server = make_server('saplh', 8000, wsgi_app)
server.serve_forever()
3、无问题后执行 之后打开http://saplh:8000 和 http://saplh:8000/?wsdl能正常显示如下地址的话表示接口成功暴露
4、使用工具测试接口,这里使用的是soapui来测试
soapui 版本5.3
工具测试无问题
5、使用sap去调用,首先现在SAP端的服务器去调用此接口,看是否可以调通,我第一次调用的时候失败了,原因是端口没开,而且没用域名而使用的ip地址的方式。
在sap服务器端测试wsdl,可以正常调通,接下来就是导入wsdl到sap内即可
python代码参考自https://www.cnblogs.com/guanfuchang/p/5985070.html
上一篇: 配置php自带的mail功能
下一篇: MR -- join操作