python调用webservice接口_webservice应用实例

python调用webservice接口_webservice应用实例最近在搞基于python的webservice项目,今天为把环境给配好,折腾了不少时间,还是把配的过程记录下来,以后备用:首先你系统上要有python,这个不必说啦,我系统上用的是2.7+其次,要用python进行webservice开发,还需要一些库:lxml:命令行下sudoeasy_installlxml就能安装pytz:命令行下sudoeasy_installpytz就…

大家好,又见面了,我是你们的朋友全栈君。如果您正在找激活码,请点击查看最新教程,关注关注公众号 “全栈程序员社区” 获取激活教程,可能之前旧版本教程已经失效.最新Idea2022.1教程亲测有效,一键激活。

Jetbrains全系列IDE使用 1年只要46元 售后保障 童叟无欺

最近在搞基于python的webservice项目,今天为把环境给配好,折腾了不少时间,还是把配的过程记录下来,以后备用:

首先你系统上要有python,这个不必说啦,我系统上用的是2.7+

其次,要用python进行webservice开发,还需要一些库:

lxml :

命令行下 sudo easy_install lxml 就能安装

pytz :

命令行下 sudo easy_install pytz 就能安装

soaplib:

进行webservice开发必须要用的库,可以在https://github.com/volador/soaplib拿到,注意要先安装上面两个插件再安装这个,因为这个依赖于上面两个插件,把zip拿下来后解压,sudo python setup.py install 就能安装了。

Soaplib is an easy to use python library for publishing soap web services using WSDL 1.1 standard, and answering SOAP 1.1 requests. With a very small amount of code, soaplib allows you to write a useful web service and deploy it as a WSGI application. 完成上面步骤后就能进行webservice发布了,看下helloworld:

直接贴代码:server.py

import soaplib

from soaplib.core.util.wsgi_wrapper import run_twisted #发布服务

from soaplib.core.server import wsgi

from soaplib.core.service import DefinitionBase #所有服务类必须继承该类

from soaplib.core.service import soap #声明注解

from soaplib.core.model.clazz import Array #声明要使用的类型

from soaplib.core.model.clazz import ClassModel #若服务返回类,该返回类必须是该类的子类

from soaplib.core.model.primitive import Integer,String

class C_ProbeCdrModel(ClassModel):

__namespace__ = “C_ProbeCdrModel”

Name=String

Id=Integer

class HelloWorldService(DefinitionBase): #this is a web service

@soap(String,_returns=String)    #声明一个服务,标识方法的参数以及返回值

def say_hello(self,name):

return ‘hello %s!’%name

@soap(_returns=Array(String))

def GetCdrArray(self):

L_Result=[“1″,”2″,”3”]

return L_Result

@soap(_returns=C_ProbeCdrModel)

def GetCdr(self):     #返回的是一个类,该类必须是ClassModel的子类,该类已经在上面定义

L_Model=C_ProbeCdrModel()

L_Model.Name=L_Model.Name

L_Model.Id=L_Model.Id

return L_Model

if __name__==’_main__’:

soap_app=soaplib.core.Application([HelloWorldService], ‘tns’)

wsgi_app=wsgi.Application(soap_app)

print ‘listening on 127.0.0.1:7789’

print ‘wsdl is at: http://127.0.0.1:7789/SOAP/?wsdl’

run_twisted( ( (wsgi_app, “SOAP”),), 7789)

if __name__==’__main__’: #发布服务

try:

from wsgiref.simple_server import make_server

soap_application = soaplib.core.Application([HelloWorldService], ‘tns’)

wsgi_application = wsgi.Application(soap_application)

server = make_server(‘localhost’, 7789, wsgi_application)

server.serve_forever()

except ImportError:

print ‘error’

python server.py可以直接运行服务了。运行服务后打开浏览器,地址栏上键入:

http://localhost:7789/SOAP/?wsdl就能看到描述服务的xml文档了。

请求服务:

需要用到suds库:

python交互模式下键入:

from suds.client import Client

test=Client(‘http://localhost:7789/SOAP/?wsdl’)

print test.service.say_hello(‘volador’)

这样就调用了say_hello这个服务了。

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/191146.html原文链接:https://javaforall.net

(0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • idea2021 激活码-激活码分享[通俗易懂]

    (idea2021 激活码)本文适用于JetBrains家族所有ide,包括IntelliJidea,phpstorm,webstorm,pycharm,datagrip等。https://javaforall.net/100143.htmlIntelliJ2021最新激活注册码,破解教程可免费永久激活,亲测有效,上面是详细链接哦~V…

    2022年3月22日
    72
  • String与Integer互相转换

    //String转换IntegerStringstr="a";Integeri=null;if(str!=null){i=Integer.valueOf(str);}//方法一:Integer类的静态方法toString():Integera=2;Stringstr=Integer.toString(a)//方法二:Integer类的…

    2022年4月6日
    66
  • MySQL + PostgreSQL批量插入更新insertOrUpdate[通俗易懂]

    MySQL + PostgreSQL批量插入更新insertOrUpdate[通俗易懂]????周周有福利,周周有惊喜哪吒社区-风火轮计划????Java学习路线配套文章:Java学习路线总结,搬砖工逆袭Java架构师(全网最强)????Java经典面试题大全:10万字208道Java经典面试题总结(附答案)????简介:Java领域优质创作者????、CSDN哪吒公众号作者✌、Java架构师奋斗者????????扫描主页左侧二维码,加入群聊,一起学习、一起进步????欢迎点赞????收藏⭐留言????这里写目录标题一、百度百科1、MySQL2、Pos

    2022年7月27日
    3
  • Mysql 练习题 及 答案

    Mysql 练习题 及 答案 –1.学生表Student(S,Sname,Sage,Ssex)–S学生编号,Sname学生姓名,Sage出生年月,Ssex学生性别–2.课程表 Course(C,Cname,T)–C–课程编号,Cname课程名称,T教师编号–3.教师表 Teacher(T,Tname)–T教师编号,Tname教师姓名–4.成绩表 SC(S,C,score)–S学生…

    2022年9月2日
    6
  • oracle soap api,Web API与SOAP API的区别

    oracle soap api,Web API与SOAP API的区别WebAPI与SOAPAPI的区别总的来说,WebAPI比SOAPAPI有优势,多数情况下SOAPAPI的优势仅在消息结构的描述上。因此在三方集成时,当对方的集成工具仅支持根据WSDL来构建调用时,才建议考虑发布SOAPAPI。1.请求消息格式不同WebAPI的消息规格为URL请求;SOAPAPI的消息格式为SOAP规范。从消息封装、编码/解码上,WebAPI有明显的并发性能优势…

    2022年7月26日
    3
  • 面试题(状态压缩dp)

    面试题(状态压缩dp)题解状态压缩dp,f[i][j]代表第i行状态为j的方案数#include<bits/stdc++.h>using namespace std;#define x first#define y second#define send string::npos#define lowbit(x) (x&(-x))#define left(x) x<<1#define right(x) x<<1|1#define transformu(s) tr..

    2022年8月9日
    5

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号