A fork of https://github.com/Synerty/SOAPpy-py3 This is a working tree till fixes get imported upstream.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

199 lines
4.8 KiB

  1. #!/usr/bin/env python
  2. #
  3. # Copyright (c) 2001 actzero, inc. All rights reserved.
  4. import sys
  5. sys.path.insert(1, "..")
  6. from SOAPpy import *
  7. # Uncomment to see outgoing HTTP headers and SOAP and incoming
  8. Config.dumpSOAPIn = 1
  9. Config.dumpSOAPOut = 1
  10. Config.debug = 1
  11. # specify name of authorization function
  12. Config.authMethod = "_authorize"
  13. # Set this to 0 to test authorization
  14. allowAll = 1
  15. # ask for returned SOAP responses to be converted to basic python types
  16. Config.simplify_objects = 1
  17. # provide a mechanism to stop the server
  18. run = 1
  19. def quit():
  20. global run
  21. run=0;
  22. if Config.SSLserver:
  23. from M2Crypto import SSL
  24. def _authorize(*args, **kw):
  25. global allowAll, Config
  26. if Config.debug:
  27. print "Authorize (function) called! (result = %d)" % allowAll
  28. print "Arguments: %s" % kw
  29. if allowAll:
  30. return 1
  31. else:
  32. return 0
  33. # Simple echo
  34. def echo(s):
  35. global Config
  36. # Test of context retrieval
  37. ctx = Server.GetSOAPContext()
  38. if Config.debug:
  39. print "SOAP Context: ", ctx
  40. return s + s
  41. # An echo class
  42. class echoBuilder2:
  43. def echo2(self, val):
  44. return val * 3
  45. # A class that has an instance variable which is an echo class
  46. class echoBuilder:
  47. def __init__(self):
  48. self.prop = echoBuilder2()
  49. def echo_ino(self, val):
  50. return val + val
  51. def _authorize(self, *args, **kw):
  52. global allowAll, Config
  53. if Config.debug:
  54. print "Authorize (method) called with arguments:"
  55. print "*args=%s" % str(args)
  56. print "**kw =%s" % str(kw)
  57. print "Approved -> %d" % allowAll
  58. if allowAll:
  59. return 1
  60. else:
  61. return 0
  62. # Echo with context
  63. def echo_wc(s, _SOAPContext):
  64. global Config
  65. c = _SOAPContext
  66. sep = '-' * 72
  67. # The Context object has extra info about the call
  68. if Config.debug:
  69. print "-- XML", sep[7:]
  70. # The original XML request
  71. print c.xmldata
  72. print "-- Header", sep[10:]
  73. # The SOAP Header or None if not present
  74. print c.header
  75. if c.header:
  76. print "-- Header.mystring", sep[19:]
  77. # An element of the SOAP Header
  78. print c.header.mystring
  79. print "-- Body", sep[8:]
  80. # The whole Body object
  81. print c.body
  82. print "-- Peer", sep[8:]
  83. if not GSI:
  84. # The socket object, useful for
  85. print c.connection.getpeername()
  86. else:
  87. # The socket object, useful for
  88. print c.connection.get_remote_address()
  89. ctx = c.connection.get_security_context()
  90. print ctx.inquire()[0].display()
  91. print "-- SOAPAction", sep[14:]
  92. # The SOAPaction HTTP header
  93. print c.soapaction
  94. print "-- HTTP headers", sep[16:]
  95. # All the HTTP headers
  96. print c.httpheaders
  97. return s + s
  98. # Echo with keyword arguments
  99. def echo_wkw(**kw):
  100. return kw['first'] + kw['second'] + kw['third']
  101. # Simple echo
  102. def echo_simple(*arg):
  103. return arg
  104. def echo_header(s, _SOAPContext):
  105. global Config
  106. c = _SOAPContext
  107. return s, c.header
  108. addr = ('localhost', 9900)
  109. GSI = 0
  110. SSL = 0
  111. if len(sys.argv) > 1 and sys.argv[1] == '-s':
  112. SSL = 1
  113. if not Config.SSLserver:
  114. raise RuntimeError, \
  115. "this Python installation doesn't have OpenSSL and M2Crypto"
  116. ssl_context = SSL.Context()
  117. ssl_context.load_cert('validate/server.pem')
  118. server = SOAPServer(addr, ssl_context = ssl_context)
  119. prefix = 'https'
  120. elif len(sys.argv) > 1 and sys.argv[1] == '-g':
  121. GSI = 1
  122. from SOAPpy.GSIServer import GSISOAPServer
  123. server = GSISOAPServer(addr)
  124. prefix = 'httpg'
  125. else:
  126. server = SOAPServer(addr)
  127. prefix = 'http'
  128. print "Server listening at: %s://%s:%d/" % (prefix, addr[0], addr[1])
  129. # register the method
  130. server.registerFunction(echo)
  131. server.registerFunction(echo, path = "/pathtest")
  132. server.registerFunction(_authorize)
  133. server.registerFunction(_authorize, path = "/pathtest")
  134. # Register a whole object
  135. o = echoBuilder()
  136. server.registerObject(o, path = "/pathtest")
  137. server.registerObject(o)
  138. # Register a function which gets called with the Context object
  139. server.registerFunction(MethodSig(echo_wc, keywords = 0, context = 1),
  140. path = "/pathtest")
  141. server.registerFunction(MethodSig(echo_wc, keywords = 0, context = 1))
  142. # Register a function that takes keywords
  143. server.registerKWFunction(echo_wkw, path = "/pathtest")
  144. server.registerKWFunction(echo_wkw)
  145. server.registerFunction(echo_simple)
  146. server.registerFunction(MethodSig(echo_header, keywords=0, context=1))
  147. server.registerFunction(quit)
  148. # Start the server
  149. try:
  150. while run:
  151. server.handle_request()
  152. except KeyboardInterrupt:
  153. pass