~brian-sidebotham/wxwidgets-cmake/wxpython-2.9.4

« back to all changes in this revision

Viewing changes to wxPython/wx/tools/Editra/src/ebmlib/e_weblib.py

  • Committer: Brian Sidebotham
  • Date: 2013-08-03 14:30:08 UTC
  • Revision ID: brian.sidebotham@gmail.com-20130803143008-c7806tkych1tp6fc
Initial import into Bazaar

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
###############################################################################
 
2
# Name: weblib.py                                                             #
 
3
# Purpose: Web an network utilties                                            #
 
4
# Author: Cody Precord <cprecord@editra.org>                                  #
 
5
# Copyright: (c) 2010 Cody Precord <staff@editra.org>                         #
 
6
# Licence: wxWindows Licence                                                  #
 
7
###############################################################################
 
8
 
 
9
"""
 
10
Editra Buisness Model Library: Web Utilities
 
11
 
 
12
Utility functions for working with web and other networking protocols
 
13
 
 
14
"""
 
15
 
 
16
__author__ = "Cody Precord <cprecord@editra.org>"
 
17
__svnid__ = "$Id: e_weblib.py 66131 2010-11-13 05:22:48Z CJP $"
 
18
__revision__ = "$Revision: 66131 $"
 
19
 
 
20
__all__ = ['SOAP12Message',]
 
21
 
 
22
#-----------------------------------------------------------------------------#
 
23
# imports
 
24
import urllib2
 
25
import httplib
 
26
 
 
27
#-----------------------------------------------------------------------------#
 
28
_SOAP_TPL = """<?xml version=\"1.0\" encoding=\"utf-8\"?>
 
29
<soap12:Envelope xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:soap12=\"http://www.w3.org/2003/05/soap-envelope\">
 
30
<soap12:Body>
 
31
    %(msg)s
 
32
</soap12:Body>
 
33
</soap12:Envelope>
 
34
 
 
35
"""
 
36
 
 
37
_SM_TPL = """<?xml version="1.0" encoding="UTF-8"?>
 
38
<SOAP-ENV:Envelope 
 
39
SOAP-ENV:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"  
 
40
xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/">
 
41
<SOAP-ENV:Body>
 
42
    %(msg)s
 
43
</SOAP-ENV:Body>
 
44
</SOAP-ENV:Envelope>
 
45
"""
 
46
 
 
47
#-----------------------------------------------------------------------------#
 
48
 
 
49
class SOAP12Message(object):
 
50
    """Class for creating and sending a message
 
51
    using the SOAP protocol.
 
52
 
 
53
    """
 
54
    def __init__(self, host, request, msg, action=""):
 
55
        """Create the message object
 
56
        @param host: host the message will be sent to (url)
 
57
        @param request: POST request
 
58
        @param msg: XML Body text
 
59
        @keyword action: SoapAction
 
60
 
 
61
        """
 
62
        assert len(host), "Must specify a valid host"
 
63
        super(SOAP12Message, self).__init__()
 
64
 
 
65
        # Attributes
 
66
        self._host = host
 
67
        self._request = request
 
68
        self._msg = msg
 
69
        self._action = action
 
70
        self._http = httplib.HTTP(self._host, 80)
 
71
 
 
72
    @property
 
73
    def MessageBody(self):
 
74
        soapmsg = _SOAP_TPL % dict(msg=self._msg)
 
75
        soapmsg = soapmsg.replace("\n", "\r\n")
 
76
        return soapmsg
 
77
 
 
78
    def Send(self):
 
79
        """Send the message"""
 
80
        # Create the SOAP message
 
81
        soapmsg = self.MessageBody
 
82
 
 
83
        # Setup Headers
 
84
        self._http.putrequest("POST", self._request)
 
85
        self._http.putheader("Host", self._host)
 
86
#        self._http.putheader("User-Agent", "Python post")
 
87
        self._http.putheader("Content-Type", "application/soap+xml; charset=utf-8")
 
88
        self._http.putheader("Content-Length", "%d" % len(soapmsg))
 
89
        self._http.putheader("SOAPAction", '"%s"' % self._action)
 
90
        self._http.endheaders()
 
91
 
 
92
        # Send it
 
93
        self._http.send(soapmsg)
 
94
 
 
95
    def GetReply(self):
 
96
        """Get the reply (may block for a long time)
 
97
        @return: (statuscode, statusmessage, header)
 
98
 
 
99
        """
 
100
        return self._http.getreply()