Calendar SOAP Server Example

From PeformIQ Upgrade
Jump to navigation Jump to search
#!/usr/bin/env python
#
#  $Id:$
#
#---------------------------------------------------------------------

import sys, httplib

#---------------------------------------------------------------------

SERVER_ADDR = "127.0.0.1"
SERVER_PORT = 8888

CAL_NS = "http://uche.ogbuji.net/ws/eg/simple-cal"

BODY_TEMPLATE = """<SOAP-ENV:Envelope
  xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/"
  xmlns:s="http://uche.ogbuji.net/eg/ws/simple-cal"
  xmlns:xsi="http://www.w3.org/1999/XMLSchema-instance"
  xmlns:xsd="http://www.w3.org/1999/XMLSchema"
  SOAP-ENV:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/">
  <SOAP-ENV:Body>

    <s:getMonth>
      <year xsi:type="xsd:integer">%s</year>
      <month xsi:type="xsd:integer">%s</month>
    </s:getMonth>
  </SOAP-ENV:Body>

</SOAP-ENV:Envelope>"""

#---------------------------------------------------------------------

def GetMonth():
    year = 2007
    month = 7
    body = BODY_TEMPLATE%(year, month)
    blen = len(body)

    requestor = httplib.HTTP(SERVER_ADDR, SERVER_PORT)

    requestor.putrequest('POST', 'cal-server')
    requestor.putheader('Host', SERVER_ADDR)
    requestor.putheader('Content-Type', 'text/plain; charset="utf-8"')
    requestor.putheader('Content-Length', str(blen))
    requestor.putheader('SOAPAction', CAL_NS)
    requestor.endheaders()
    requestor.send(body)

    (status_code, message, reply_headers) = requestor.getreply()

    reply_body = requestor.getfile().read()

    print "status code:", status_code
    print "status message:", message
    print "HTTP reply body:\n", reply_body


#---------------------------------------------------------------------

if __name__ == "__main__":
    GetMonth()

#---------------------------------------------------------------------
#!/usr/bin/env python
#
#  $Id:$
#
#---------------------------------------------------------------------

import sys, calendar

#Import the SOAP.py machinery
#from WebServices import SOAP

import SOAPpy

#---------------------------------------------------------------------

CAL_NS = "http://uche.ogbuji.net/eg/ws/simple-cal"

#---------------------------------------------------------------------

class Calendar:
  def getMonth(self, year, month):
    return calendar.month(year, month)

  def getYear(self, year):
    return calendar.calendar(year)

#---------------------------------------------------------------------

server = SOAPpy.SOAPServer(("localhost", 8888))
cal    = Calendar()

server.registerObject(cal, CAL_NS)

print "Starting server..."

server.serve_forever()

#---------------------------------------------------------------------