用soaplib的django webserver
前面写过怎么利用suds来调用webservicePython调用基于https协议的SOAP WebService,这篇讲的是如何用soaplib开发SOAP WebService(最近发现国外开源社区里把json方式的别的Web服务也叫做WebService,叫法跟Java和.Net的约定叫法不太一样,这里加上SOAP以跟json格式的WebService区分开来)。
第一步,当然是安装问题了:
下载soaplib:
这是下载地址,我选了soaplib 2.0.0-beta2下载,因为接下来要用到一个djangosnippet是基于2.0的。
在开发环境(Mac OS X 10.7.4)上安装很顺利:
$ python setup.py install
但在测试环境(CentOS)上却碰到了一点麻烦,执行上述安装时报错:error: Setup script exited with error: command 'gcc' failed with exit status 1。网上找到有人提出的解决方案是安装libxml2, libxml2-devel, libxslt-devel,使用yum安装之。
$yum install libxml2
$yum install libxml2-devel
$yum install libxslt-devel
这里有个小技巧,一般查到debian下安装这类包时使用apt-get,而CentOS使用yum,当看到以apt-get的方式安装的时叫apt-get install libxml2-dev之类的,只要把把dev换成devel,就可以使用yum在centos或者redhat下安装了,但是这次我上了个当,网上查到好几个地方的libxslt安装都是apt-get install libxslt1-dev,但是yum包没有那个“1”。。。后来我去下载lxml2.3.4源码包,看到下面那一段话
* On **Linux** (and most other well-behaved operating systems), ``easy_install`` will manage to build the source distribution as long as libxml2 and libxslt are properly installed, including development packages, i.e. header files, etc. Use your package management tool to look for packages like ``libxml2-dev`` or ``libxslt-devel`` if the build fails, and make sure they are installed.
才去掉那个“1”,安装成功。
接下来去下载一个叫SOAP web service with soaplib 2.0的东东,因为最近刚过儿童节,风紧,据说好多org域名的境外网站貌似都不问青红皂白被墙了,把代码贴出如下:
from soaplib.core import Application
from soaplib.core.server.wsgi import Application as WSGIApplication
from django.http import HttpResponse class DjangoSoapApp(WSGIApplication):
"""
Generic Django view for creating SOAP web services (works with soaplib 2.0) Based on http://djangosnippets.org/snippets/2210/
""" csrf_exempt = True def __init__(self, services, tns):
"""Create Django view for given SOAP soaplib services and tns""" return super(DjangoSoapApp, self).__init__(Application(services, tns)) def __call__(self, request):
django_response = HttpResponse() def start_response(status, headers):
django_response.status_code = int(status.split(' ', 1)[0])
for header, value in headers:
django_response[header] = value response = super(DjangoSoapApp, self).__call__(request.META, start_response)
django_response.content = '\n'.join(response) return django_response
djangosnippets.org是个好网站,有很多很有用的django小部件,缺点是,容易莫名其妙被墙,以及Usage太简单,很难偷懒不阅读他的源码直接使用,这个snippet的Usage就只有一行字:
Usage is the same as before: my_soap_service = DjangoSoapApp([MySOAPService], __name__)
它之前的版本也非常简单,而且那些引用在这个版本里都已经移动位置了,不得已,只得去阅读soaplib2.0的源码,把相应的位置纠正过来。
我来写个比他详细点的Usage吧:
View.py里的代码:
#这几个引用,soablib2.0的位置跟0.9+之类的版本不一样了
from soaplib.core.model.primitive import Boolean, String
from soaplib.core.service import DefinitionBase, rpc # the class with actual web methods
class MySOAPService(DefinitionBase):
@rpc(String, String, _returns=Boolean)
def Test(self, f1, f2):
return True
@rpc(String, _returns=String)
def HelloWorld(self, name):
return 'Hello %s!' %name my_soap_service = DjangoSoapApp([MySOAPService], 'laonan.net')
urls.py里的代码:
url(r'^my-soap-service/service', 'yourproject.yourapp.views.my_soap_service'),
url(r'^my-soap-service/service.wsdl', 'yourproject.yourapp.views.my_soap_service'),
上面这两个地址一定要配对,最好到wsdl文档里去确认下soap:address节点的location属性,最早我配的时候,第一个url少配了service那一段,还以为是soaplib2是beta版有bug,怎么老找不到,查了半天才发现是这里配错了。
下面来测试下:
(1)安装suds
安装python
下载setuptools-0.6c9.tar.gz
#python setup.py install
下载python-suds-0.4.tar.gz
#python setup.py install
进行python命令行
>>> from suds.client import Client
>>> hello_client = Client('http://127.0.0.1:8080/my-soap-service/service
/?wsdl')
>>> result = hello_client.service.HelloWorld("carlos")
>>> print result
Hello carlos
用soaplib的django webserver的更多相关文章
- python发布及调用基于SOAP的webservice
现如今面向服务(SOA)的架构设计已经成为主流,把公用的服务打包成一个个webservice供各方调用是一种非常常用的做法,而应用最广泛的则是基于SOAP协议和wsdl的webservice.本文讲解 ...
- 异步任务队列Celery在Django中的使用
前段时间在Django Web平台开发中,碰到一些请求执行的任务时间较长(几分钟),为了加快用户的响应时间,因此决定采用异步任务的方式在后台执行这些任务.在同事的指引下接触了Celery这个异步任务队 ...
- 为什么不能访问django自带的索引页
通过HTTP://192.168.160.128:8000访问虚拟机上的django索引页出现“ 无法访问此网站 192.168.160.128 拒绝了我们的连接请求. ” 是什么原因呢?费了好大一番 ...
- django自带wsgi server vs 部署uwsgi+nginx后的性能对比
一.下面先交代一下测试云主机 cpu: root@alexknight:/tmp/webbench-1.5# cat /proc/cpuinfo |grep model model : model n ...
- Setting up Django and your web server with uWSGI and nginx
https://uwsgi.readthedocs.io/en/latest/tutorials/Django_and_nginx.html Setting up Django and your we ...
- Docker distrubution in django
https://www.syncano.io/blog/configuring-running-django-celery-docker-containers-pt-1/ Update: Fig ha ...
- 第二天 django apache
1. 475 brew tap homebrew/apache 476 brew install mod_wsgi 477 ls /usr/local/Cellar/mod_wsgi/4.5.7 ...
- Cenos(6.6/7.1)下从源码安装Python+Django+uwsgi+nginx到写nginx的环境部署(一)
梳理下这几个的关系: centos是redhat的社区版操作系统. Python2.7.5是开发语言(centos6.5下自带的python是2.6.6版本,所以需要源码更新,而centos7.1下面 ...
- 两个Python web框架:Django & Tornado比较
就是说它作为 web 框架比 Django 简单,又支援异步 IO,且更不需要前端的 webserver ? 我已经混乱了, Tornado是 Nginx.Django.Node.js 的结合体?又或 ...
随机推荐
- 【Android Studio】No JVM installation found
如果没有配置好JDK的环境变量,启动Android Studio的时候会报错: 请参考我整理的博客文章<JDK的下载.安装和配置>,链接:http://www.cnblogs.com/du ...
- hdoj 2085 核反应堆【水】
核反应堆 Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submis ...
- jQuery效果:隐藏、显示、切换、滑动、淡入淡出、动画
jQuery效果 隐藏.显示.切换.滑动.淡入淡出.以及动画 1.隐藏与显示(改变:display:none;) hide()--隐藏 show()--显示 toggle()方法:可以使用它来切换hi ...
- 看小白如何解决ajax跨域问题
由于此前很少写前端的代码(哈哈,不合格的程序员啊),最近项目中用到json作为系统间交互的手段,自然就伴随着众多ajax请求,随之而来的就是要解决ajax的跨域问题.本篇将讲述一个小白从遇到跨域不知道 ...
- 全情投入是做好工作的基础——Leo鉴书39
很多人都有:“内向的人则不擅长社交,只能会活得很封闭”的思想,于是不少内向的朋友要么认为只有扭曲自己的性格变得外向才能在社会上吃得开,才能很爽的行走职场:要么就决定完全封闭自己活在孤独之中,其实以上两 ...
- 关于Form窗体的StartPosition 属性如何设置的问题
1.让窗体在启动时在指定位置出现 form1.StartPosition Manual CenterScreen WindowsDefaultLocation (default) WindowsDef ...
- [Javascript] Using console.count to Count Events
Learn how to user console.count in order to log out how many times a given thing has happened. ; i & ...
- BAT染指影视制作 欲全面撬开互联网粉丝经济
预測: 或靠"用户"模式盈利 除了内容制作,电影发行也在遭遇互联网模式的冲击. 除了给片方支付高额保底以外,随着市场竞争激烈.新进入者都在争夺好片的发行权. 业内预測.再往后,发行 ...
- 关于Build Active Architecture Only属性
关于Build Active Architecture Only属性 Architecture 属性在BuildSetting里. 这个属性设置为yes,是为了debug的时候编译速度更快,它只编译当 ...
- JQ插件之imgAreaSelect实现对图片的在线截图功能(java版)
前言:在做网站的时候经常用的功能就是,用户上传图片对自己上传的图片进行截图,DIV自己的头像.或者上传幻灯片大图进行DIV设置小图. 解决方案:目前我知道的解决方案有两个如下: 一.fla ...