Passion/Python

SOAP 웹서비스콜을 naive하게 부르기

sunshout 2009. 10. 21. 22:54

(Language : python)
  1. import sys, httplib
  2.  
  3. # a "as lighter as possible" soap message:
  4.  
  5. SM_TEMPLATE = """<?xml version="1.0" encoding="UTF-8"?>
  6. <SOAP-ENV:Envelope
  7. SOAP-ENV:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/
  8. xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/">
  9. <SOAP-ENV:Body>
  10. <ns1:readLS xmlns:ns1="http://phonedirlux.homeip.net/types">
  11. <String_1>%s</String_1>
  12. </ns1:readLS>
  13. </SOAP-ENV:Body>
  14. </SOAP-ENV:Envelope>
  15. """
  16.  
  17. SoapMessage = SM_TEMPLATE%("Your message or e-mail")
  18.  
  19. print SoapMessage
  20.  
  21. #construct and send the header
  22.  
  23. webservice = httplib.HTTP("www.pascalbotte.be")
  24. webservice.putrequest("POST", "/rcx-ws/rcx")
  25. webservice.putheader("Host", "www.pascalbotte.be")
  26. webservice.putheader("User-Agent", "Python post")
  27. webservice.putheader("Content-type", "text/xml; charset=\"UTF-8\"")
  28. webservice.putheader("Content-length", "%d" % len(SoapMessage))
  29. webservice.putheader("SOAPAction", "\"\"")
  30. webservice.endheaders()
  31. webservice.send(SoapMessage)
  32.  
  33. # get the response
  34.  
  35. statuscode, statusmessage, header = webservice.getreply()
  36. print "Response: ", statuscode, statusmessage
  37. print "headers: ", header
  38. res = webservice.getfile().read()
  39. print res

Python에서 SOAPpy를 이용하여 SOAP을 부를수도 있지만, 생성된  XML 코드가 일치하지 않아서 제대로 동작하지 않는 경우가 있다.

이를 경우에는 전달되는 SOAP 메시지를 분석하게 나이브하게 메시지를 만들고 httplib를 이용하여 메소드를 콜 할 수 있다.