python实现webservice的几种方法
时间: 2024-02-05 08:04:08 浏览: 82
以下是Python实现WebService的三种方法:
1.使用Python自带的模块suds库实现WebService。
```python
from suds.client import Client
url = 'http://www.webxml.com.cn/WebServices/WeatherWebService.asmx?wsdl'
client = Client(url)
result = client.service.getWeatherbyCityName('北京')
print(result)
```
2.使用Python自带的模块zeep库实现WebService。
```python
from zeep import Client
url = 'http://www.webxml.com.cn/WebServices/WeatherWebService.asmx?wsdl'
client = Client(url)
result = client.service.getWeatherbyCityName('北京')
print(result)
```
3.使用第三方库spyne实现WebService。
```python
from spyne import Application, rpc, ServiceBase, Integer, Unicode
from spyne.protocol.soap import Soap11
from spyne.server.wsgi import WsgiApplication
class HelloWorldService(ServiceBase):
@rpc(Unicode, Integer, _returns=Unicode)
def say_hello(ctx, name, times):
return 'Hello, %s' % name * times
application = Application([HelloWorldService], tns='spyne.examples.hello', in_protocol=Soap11(validator='lxml'), out_protocol=Soap11())
wsgi_application = WsgiApplication(application)
if __name__ == '__main__':
import logging
from wsgiref.simple_server import make_server
logging.basicConfig(level=logging.DEBUG)
logging.getLogger('spyne.protocol.xml').setLevel(logging.DEBUG)
logging.info("listening to http://127.0.0.1:8000")
logging.info("wsdl is at: http://localhost:8000/?wsdl")
server = make_server('127.0.0.1', 8000, wsgi_application)
server.serve_forever()
```
阅读全文