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.
 
 
 
 

150 lines
4.4 KiB

  1. #!/usr/bin/env python
  2. # Copyright (c) 2001, actzero, inc.
  3. import sys
  4. sys.path.insert(1,"..")
  5. from SOAPpy import SOAP
  6. #SOAP.Config.debug = 1
  7. serverstring = "SOAP.py (actzero.com) running "+sys.platform
  8. NUMBUYS = 0
  9. NUMSIMPLEBUYS = 0
  10. NUMREQUESTS = 0
  11. NUMPINGS = 0
  12. def SimpleBuy(Address, ProductName, Quantity):
  13. # currently, this type-checks the params, and makes sure
  14. # the strings are of len > 0
  15. global NUMSIMPLEBUYS
  16. NUMSIMPLEBUYS += 1
  17. if Quantity < 1: raise ValueError, "must order at least one"
  18. else: return "Receipt for %d %s(s) bought from %s" % (int(Quantity), ProductName, serverstring)
  19. def RequestForQuote(ProductName, Quantity):
  20. # type-checks and makes sure Quantity >= 1
  21. global NUMREQUESTS
  22. NUMREQUESTS += 1
  23. if Quantity < 1: raise ValueError, "must order at least 1"
  24. else:
  25. import whrandom
  26. mult = whrandom.random()
  27. times = 0
  28. while mult > 0.25:
  29. mult = mult - 0.25
  30. times += 1
  31. mult += 0.5
  32. mult = round(mult, 3)
  33. print mult, times
  34. return SOAP.doubleType(round(mult*int(Quantity),2))
  35. def Buy(**kw):
  36. global NUMBUYS
  37. NUMBUYS += 1
  38. try:
  39. PurchaseOrder = kw["PurchaseOrder"]
  40. except:
  41. PurchaseOrder = kw["PO"]
  42. try:
  43. POkeys = PurchaseOrder['_keyord']
  44. POkeys.sort()
  45. POkeys_expected = ["shipTo","billTo","items","poID","createDate"]
  46. POkeys_expected.sort()
  47. if POkeys != POkeys_expected:
  48. raise ValueError, "struct 'PurchaseOrder' needs %s, %s, %s, %s, and %s" % tuple(POkeys_expected)
  49. except:
  50. raise TypeError, "'PurchaseOrder' missing one or more element(s)"
  51. try:
  52. btkeys = PurchaseOrder["billTo"]["_keyord"]
  53. btkeys.sort()
  54. btkeys_expected = ["address","zipCode","name","state","city"]
  55. btkeys_expected.sort()
  56. except:
  57. raise TypeError, "'billTo' missing one or more elements"
  58. try:
  59. stkeys = PurchaseOrder["shipTo"]["_keyord"]
  60. stkeys.sort()
  61. stkeys_expected = ["address","zipCode","name","state","city"]
  62. stkeys_expected.sort()
  63. except:
  64. raise TypeError, "'shipTo' missing one or more elements"
  65. try:
  66. items = PurchaseOrder["items"].__dict__
  67. data = items["data"]
  68. retstring = ""
  69. for item in data:
  70. itemdict = item["_asdict"]
  71. q = itemdict["quantity"]
  72. p = itemdict["price"]
  73. name = itemdict["name"]
  74. if retstring != "":
  75. retstring += ", "
  76. else:
  77. retstring = "bought "
  78. retstring += "%d %s(s) for %.2f" % (q,name,p)
  79. retstring += " from "+serverstring
  80. return retstring
  81. except:
  82. raise TypeError, "items must be an array of 'item' structs"
  83. def Ping():
  84. global NUMPINGS
  85. NUMPINGS += 1
  86. return
  87. def Monitor(str):
  88. if str=="actzero":
  89. global NUMBUYS
  90. global NUMREQUESTS
  91. global NUMSIMPLEBUYS
  92. global NUMPINGS
  93. return "(Buys, RequestForQuote(s),SimpleBuy(s), Ping(s)) = " + \
  94. repr( (NUMBUYS,NUMREQUESTS,NUMSIMPLEBUYS, NUMPINGS) )
  95. else:
  96. raise ValueError, "not the right string"
  97. def Clear(str):
  98. if str=="actzero":
  99. global NUMBUYS
  100. global NUMREQUESTS
  101. global NUMSIMPLEBUYS
  102. global NUMPINGS
  103. NUMBUYS = 0
  104. NUMREQUESTS = 0
  105. NUMSIMPLEBUYS = 0
  106. NUMPINGS = 0
  107. return "(Buys, RequestForQuote(s),SimpleBuy(s), Ping(s)) = " + \
  108. repr( (NUMBUYS,NUMREQUESTS,NUMSIMPLEBUYS, NUMPINGS) )
  109. else:
  110. raise ValueError, "not the right string"
  111. if __name__ == "__main__":
  112. if len(sys.argv) > 1:
  113. try:
  114. port = int(sys.argv[1])
  115. if port not in range(2000,15000): raise ValueError
  116. except:
  117. print "port must be a number between 2000 and 15000"
  118. sys.exit(1)
  119. else: port = 9000
  120. namespace = "http://www.soapinterop.org/Bid"
  121. server = SOAP.SOAPServer( ('zoo',port) )
  122. server.registerKWFunction(SimpleBuy, namespace )
  123. server.registerKWFunction(RequestForQuote, namespace )
  124. server.registerKWFunction(Buy, namespace )
  125. server.registerKWFunction(Ping, namespace )
  126. server.registerKWFunction(Monitor, namespace )
  127. server.registerKWFunction(Clear, namespace )
  128. try:
  129. server.serve_forever()
  130. except KeyboardInterrupt:
  131. pass