Last active
February 23, 2021 23:35
-
-
Save plq/4970712 to your computer and use it in GitHub Desktop.
Spyne Example
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| from suds.client import Client | |
| url = 'http://localhost:7789/?wsdl' | |
| company = '2CA' | |
| store = 1 | |
| password = '1234' | |
| client = Client(url) | |
| client.service.GetTicketList( 'company_name', 1, '1234' ) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/usr/bin/python | |
| ''' | |
| # Code to test the GetTicketList service function: | |
| from suds.client import Client | |
| url = 'http://10.9.0.15:7789/?wsdl' | |
| company = '2CA' | |
| store = 1 | |
| password = '1234' | |
| client = Client(url) | |
| client.service.GetTicketList( 'company_name', 1, '1234' ) | |
| ''' | |
| import sys | |
| import os | |
| from subprocess import * | |
| import logging | |
| from spyne.decorator import srpc | |
| from spyne.service import ServiceBase | |
| from spyne.model.complex import ComplexModel | |
| from spyne.model.complex import Array | |
| from spyne.model.primitive import Integer | |
| from spyne.model.primitive import String | |
| from spyne.model.primitive import Boolean | |
| from spyne.model.primitive import Mandatory | |
| from spyne.model.primitive import Float | |
| from spyne.util.simple import wsgi_soap_application | |
| soap_namespace = 'http://olympuspos.net' | |
| def run_cmd(cmd): | |
| p = Popen(cmd, shell=True, stdout=PIPE) | |
| output = p.communicate()[0] | |
| return output | |
| def get_ip(): | |
| result = run_cmd( 'ip addr' ) | |
| lines = result.split('\n') | |
| skip = True | |
| ip = '' | |
| for line in lines: | |
| if 'BROADCAST' in line: | |
| skip = False | |
| if skip: | |
| continue | |
| if 'inet' in line: | |
| a, address, b, mask, c, d, interface = line.split() | |
| ip, net = address.split('/') | |
| break | |
| return ip | |
| class TicketEntry(ComplexModel): | |
| __namespace__ = soap_namespace | |
| ticketEntryNum = String | |
| itemId = Integer | |
| ArrayOfTicketEntry = Array( TicketEntry ) | |
| ArrayOfTicketEntry.__type_name__ = 'ArrayOfTicketEntry' | |
| class Ticket(ComplexModel): | |
| __namespace__ = soap_namespace | |
| ticketNum = String | |
| register = String | |
| employeeID = Integer | |
| Entries = ArrayOfTicketEntry | |
| class TicketListing(ComplexModel): | |
| __namespace__ = soap_namespace | |
| ticketNum = String | |
| register = String | |
| employeeName = String | |
| ArrayOfTicketList = Array(TicketListing) | |
| ArrayOfTicketList.__type_name__ = 'ArrayOfTicketList' | |
| class TicketListResult(ComplexModel): | |
| __namespace__ = soap_namespace | |
| success = Boolean | |
| Tickets = ArrayOfTicketList | |
| class AgentSoapService(ServiceBase): | |
| #__service_name__ The class name will be made public in the wsdl document unless explicitly overridden | |
| # | |
| # GetTicketList | |
| # | |
| @srpc(Mandatory.String, Mandatory.Integer, Mandatory.String, _returns=TicketListResult ) | |
| def GetTicketList( company, store, storePassword ): | |
| r = TicketListResult() | |
| r.success = True | |
| if not r.success: | |
| print message | |
| tickets = [] | |
| if r.success: | |
| for ticketNum in all_tickets: | |
| ticket = all_tickets[ ticketNum ] | |
| tl = TicketListing() | |
| tl.ticketNum = ticket.ticketNum | |
| tl.register = ticket.register | |
| tl.employeeName = "CEO Robert" | |
| tickets.append( tl ) | |
| r.Tickets = tickets | |
| return r | |
| if 1: # Setup fake data for GetTicketList | |
| te1 = TicketEntry() | |
| te1.ticketEntryNum = '1' | |
| te1.itemId = 42 | |
| te2 = TicketEntry() | |
| te2.ticketEntryNum = '2' | |
| te2.itemId = 62 | |
| t = Ticket() | |
| t.ticketNum = '2000' | |
| t.register = '0' | |
| t.employeeID = 6 | |
| t.Entries = [ te1, te2 ] | |
| all_tickets = { t.ticketNum:t } | |
| if __name__=='__main__': | |
| from wsgiref.simple_server import make_server | |
| logging.basicConfig(level=logging.DEBUG) | |
| logging.getLogger('spyne.protocol.xml').setLevel(logging.DEBUG) | |
| ip = get_ip() | |
| if not len( ip ): | |
| exit () | |
| logging.info( "listening to http://%s:7789" % ip ) | |
| logging.info( "wsdl is at: http://%s:7789/?wsdl" % ip ) | |
| wsgi_app = wsgi_soap_application([AgentSoapService], soap_namespace, validator='lxml' ) | |
| server = make_server(ip, 7789, wsgi_app) | |
| server.serve_forever() | |
| print "listening to http://%s:7789" % ip | |
| print "wsdl is at: http://%s:7789/?wsdl" % ip |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment