Skip to content

Instantly share code, notes, and snippets.

@wowkin2
Forked from Apkawa/daemon.py
Last active August 29, 2015 14:24
Show Gist options
  • Save wowkin2/d272ada7d4b260925ed5 to your computer and use it in GitHub Desktop.
Save wowkin2/d272ada7d4b260925ed5 to your computer and use it in GitHub Desktop.
'''
Simple usage as cherrypy daemon as example
'''
import soaplib
from soaplib.core.server import wsgi
from cherrypy.wsgiserver import CherryPyWSGIServer
HOST = "0.0.0.0"
PORT = 45124
if __name__ == '__main__':
try:
soap_application = soaplib.core.Application([MySoapService], 'tns')
wsgi_application = wsgi.Application(soap_application)
server = CherryPyWSGIServer((HOST, PORT),
wsgi_application,
numthreads=10,
server_name=None,
max=-1,
request_queue_size=10,
timeout=100)
server.start()
except KeyboardInterrupt:
server.stop()
print "Shutdown."
# -*- coding: utf-8 -*-
'''
documentation in http://soaplib.github.com/soaplib/2_0/
'''
import base64
import soaplib
from soaplib.core import Application
from soaplib.core.model import primitive as soap_types
from soaplib.core.service import DefinitionBase
from soaplib.core.service import rpc as soapmethod
from soaplib.core.server import wsgi
from soaplib.core.model.clazz import ClassModel
from soaplib.core.model.clazz import Array
from django.http import HttpResponse
# the class with actual web methods
# the class which acts as a wrapper between soaplib WSGI functionality and Django
class DjangoSoapApp(wsgi.Application):
def __call__(self, request):
# wrap the soaplib response into a Django response object
django_response = HttpResponse()
def start_response(status, headers):
status, reason = status.split(' ', 1)
django_response.status_code = int(status)
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
class SoapView(DefinitionBase):
@classmethod
def as_view(cls):
soap_application = Application([cls], __name__)
return DjangoSoapApp(soap_application)
# the view to use in urls.py
#my_soap_service = DjangoSoapApp([MySOAPService], __name__)
# -*- coding: utf-8 -*-
from django.conf.urls.defaults import patterns, include, url
import views
urlpatterns = patterns('',
url(r'^soap/', views.my_soap_service),
)
# -*- coding: utf-8 -*-
from soap import SoapView
from soap import soapmethod
from soap import soap_types, Array
from soap import ClassModel
class Item(ClassModel):
__namespace__ = 'warehouse'
pk = soap_types.Integer
name = soap_types.String
class MySoapService(SoapView):
__tns__ = '[url]http://localhost/my-soap-service/[/url]'
@soapmethod(soap_types.String, _returns=soap_types.String)
def say_hello(self, name):
results = 'Hello, %s' %name
return results
@soapmethod(_returns=Array(Warehouse))
def get_items(self):
return [Item(pk=1, name='Item 1'), Item(pk=2, name='Item 2')]
my_soap_service = MySoapService.as_view()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment