SOAP Simple Object Access Protocol 20071030 I Distributed
SOAP: Simple Object Access Protocol 2007/10/30 최석훈
목차 I. Distributed System II. SOAP의 장점 III. SOAP 메시지 구조 I. Envelope II. Header III. Body IV. SOAP 예제 – 달력 얻기 I. SOAP server II. SOAP client III. Fault element
SOAP 메시지 구조 <? XML VERSION="1. 0" ENCODING="EUC-KR" ? > <Envelope> <!-- Header 부분: 생략 가능 --> <Header> … </Header> <!-- Body 부분: 필수 --> <Body> … </Body> </Envelope>
Envelope
Envelope element �루트 엘리먼트 �SOAP 메시지의 네임스페이스를 지정 �http: //schemas. xmlsoap. org/soap/envelope/ �http: //www. w 3. org/2003/05/soap-envelope/ �encoding. Style attribute �어떤 식으로 메시지를 구성할 것인지 명시 �http: //schemas. xmlsoap. org/soap/encoding/ �http: //www. w 3. org/2003/05/soap-encoding
Header
Header element �actor attribute (role attribute in SOAP 1. 2) �해당 헤더를 어떤 수신자가 처리할 것인지 기술 <? XML VERSION="1. 0" ENCODING="EUC-KR" ? > <en: Envelope …> <en: Header> <au: Authentication xmlns: au="http: //. . . " en: actor="http: //actor. com/auth" en: must. Understand="1"> <id>foo</id> <pw>1 q 2 w 3 e</pw> </au: authentication> </en: Header> … </Envelope> <actor. com에서 처리될 메시지> <? XML VERSION="1. 0" ENCODING="EUC-KR" ? > <en: Envelope …> <en: Header> <ao: Authentication. Ok xmlns: ao="http: //. . . "> <name>Gildong, Hong. </name> </ao: Authentication. Ok> </en: Header> … </Envelope> <actor. com에서 처리한 결과 메시지>
Body
SOAP 서버 1. 2. 3. 4. 5. 6. 7. 8. 9. 10. 11. 12. 13. 14. 15. 16. 17. 18. 19. 20. 21. 22. #!/usr/bin/env python import sys, calendar #Import the SOAP. py machinery from SOAPpy import SOAP CAL_NS = "http: //uche. ogbuji. net/eg/ws/simple-cal" class Calendar: def get. Month(self, year, month): return calendar. month(year, month) def get. Year(self, year): return calendar(year) server = SOAPServer(("localhost", 8080)) cal = Calendar() server. register. Object(cal, CAL_NS) print "Starting server. . . " server. serve_forever()
SOAP 클라이언트 1. 2. 3. 4. 5. 6. 7. 8. 9. 10. 11. 12. 13. 14. 15. 16. 17. 18. 19. import sys, httplib SERVER_ADDR = '127. 0. 0. 1' SERVER_PORT = 8080 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. w 3. org/1999/XMLSchema-instance" xmlns: xsd="http: //www. w 3. org/1999/XMLSchema" SOAP-ENV: encoding. Style="http: //schemas. xmlsoap. org/soap/encoding/"> <SOAP-ENV: Body> <s: get. Month> <year xsi: type="xsd: integer">%s</year> <month xsi: type="xsd: integer">%s</month> </s: get. Month> </SOAP-ENV: Body> </SOAP-ENV: Envelope>"""
SOAP 클라이언트(계속) 20. 21. 22. 23. 24. 25. 26. 27. 28. 29. 30. 31. 32. 33. 34. 35. 36. 37. 38. def Get. Month(): year = 2001 month = 12 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", "http: //uche. ogbuji. net/eg/ws/simple-cal") requestor. endheaders() print 'SENDED MESSAGE---' print body print '---------n' requestor. send(body)
HTTP headers �Accept �Specifies which Internet media types are acceptable �Content-Length �Indicates the size (in octets) of the entity-body �Content-Type �Specifies the Internet media type of the entity-body �Host �Specifies the Internet host and port number �User-Agent �Contains information about the user agent (client) �SOAPAction �URI or empty string("") or NULL
SOAP 클라이언트(계속) 20. 21. 22. 23. 24. 25. 26. 27. 28. 29. 30. 31. 32. 33. 34. 35. 36. 37. 38. def Get. Month(): year = 2001 month = 12 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/xml; charset="utf-8"") requestor. putheader("Content-Length", str(blen)) requestor. putheader("SOAPAction", "http: //uche. ogbuji. net/eg/ws/simple-cal") requestor. endheaders() print 'SENDED MESSAGE---' print body print '---------n' requestor. send(body)
SOAP 클라이언트(계속) 39. (status_code, message, reply_headers) = requestor. getreply() 40. 41. reply_body = requestor. getfile(). read() 42. 43. print 'RECEIVED MESSAGE---' 44. print "status code: ", status_code 45. print "status message: ", message 46. print "HTTP reply header: " 47. for header in reply_headers: 48. print header, 49. print "HTTP reply body: n", reply_body 50. print '----------' 51. 52. if __name__ == "__main__": 53. Get. Month()
HTTP status code �Successful 2 xx � 200 – OK / 204 – No Content �Redirection 3 xx � 301 – Moved Permanently / 302 – Found �Client Error 4 xx � 400 – Bad Request / 403 – Forbidden / 404 – Not Found �Server Error 5 xx � 500 – Internal Server Error / 503 – Service Unavailable
Fault
SOAP fault
Reference / Q&A �석광진 외 역, 자바를 이용한 웹 서비스 구축. �안진만 역, . NET XML 웹 서비스. �김종민 외 저, POWER XML. �송은지 외 저, 웹 서비스 컴퓨팅. �Quick reference to HTTP headers �The Python Web services developer: Python SOAP libraries �SOAP Version 1. 2 Part 1: Messaging Framework (Second Edition) �images from gettyimages RF section
- Slides: 26