Package suds :: Package transport :: Module http
[hide private]
[frames] | no frames]

Source Code for Module suds.transport.http

  1  # This program is free software; you can redistribute it and/or modify 
  2  # it under the terms of the (LGPL) GNU Lesser General Public License as 
  3  # published by the Free Software Foundation; either version 3 of the  
  4  # License, or (at your option) any later version. 
  5  # 
  6  # This program is distributed in the hope that it will be useful, 
  7  # but WITHOUT ANY WARRANTY; without even the implied warranty of 
  8  # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the 
  9  # GNU Library Lesser General Public License for more details at 
 10  # ( http://www.gnu.org/licenses/lgpl.html ). 
 11  # 
 12  # You should have received a copy of the GNU Lesser General Public License 
 13  # along with this program; if not, write to the Free Software 
 14  # Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. 
 15  # written by: Jeff Ortel ( jortel@redhat.com ) 
 16   
 17  """ 
 18  Contains classes for basic HTTP transport implementations. 
 19  """ 
 20   
 21  import urllib2 as u2 
 22  import base64 
 23  import socket 
 24  from suds.transport import * 
 25  from suds.properties import Unskin 
 26  from urlparse import urlparse 
 27  from cookielib import CookieJar 
 28  from logging import getLogger 
 29   
 30  log = getLogger(__name__) 
 31   
 32   
33 -class HttpTransport(Transport):
34 """ 35 HTTP transport using urllib2. Provided basic http transport 36 that provides for cookies, proxies but no authentication. 37 """ 38
39 - def __init__(self, **kwargs):
40 """ 41 @param kwargs: Keyword arguments. 42 - B{proxy} - An http proxy to be specified on requests. 43 The proxy is defined as {protocol:proxy,} 44 - type: I{dict} 45 - default: {} 46 - B{timeout} - Set the url open timeout (seconds). 47 - type: I{float} 48 - default: 90 49 """ 50 Transport.__init__(self) 51 Unskin(self.options).update(kwargs) 52 self.cookiejar = CookieJar() 53 self.proxy = {} 54 self.urlopener = None
55
56 - def open(self, request):
57 try: 58 url = request.url 59 log.debug('opening (%s)', url) 60 u2request = u2.Request(url) 61 self.proxy = self.options.proxy 62 return self.u2open(u2request) 63 except u2.HTTPError, e: 64 raise TransportError(str(e), e.code, e.fp)
65
66 - def send(self, request):
67 result = None 68 url = request.url 69 msg = request.message 70 headers = request.headers 71 try: 72 u2request = u2.Request(url, msg, headers) 73 self.addcookies(u2request) 74 self.proxy = self.options.proxy 75 request.headers.update(u2request.headers) 76 log.debug('sending:\n%s', request) 77 fp = self.u2open(u2request) 78 self.getcookies(fp, u2request) 79 result = Reply(200, fp.headers.dict, fp.read()) 80 log.debug('received:\n%s', result) 81 except u2.HTTPError, e: 82 if e.code in (202,204): 83 result = None 84 else: 85 raise TransportError(e.msg, e.code, e.fp) 86 return result
87
88 - def addcookies(self, u2request):
89 """ 90 Add cookies in the cookiejar to the request. 91 @param u2request: A urllib2 request. 92 @rtype: u2request: urllib2.Requet. 93 """ 94 self.cookiejar.add_cookie_header(u2request)
95
96 - def getcookies(self, fp, u2request):
97 """ 98 Add cookies in the request to the cookiejar. 99 @param u2request: A urllib2 request. 100 @rtype: u2request: urllib2.Requet. 101 """ 102 self.cookiejar.extract_cookies(fp, u2request)
103
104 - def u2open(self, u2request):
105 """ 106 Open a connection. 107 @param u2request: A urllib2 request. 108 @type u2request: urllib2.Requet. 109 @return: The opened file-like urllib2 object. 110 @rtype: fp 111 """ 112 tm = self.options.timeout 113 url = self.u2opener() 114 if self.u2ver() < 2.6: 115 socket.setdefaulttimeout(tm) 116 return url.open(u2request) 117 else: 118 return url.open(u2request, timeout=tm)
119
120 - def u2opener(self):
121 """ 122 Create a urllib opener. 123 @return: An opener. 124 @rtype: I{OpenerDirector} 125 """ 126 if self.urlopener is None: 127 return u2.build_opener(*self.u2handlers()) 128 else: 129 return self.urlopener
130
131 - def u2handlers(self):
132 """ 133 Get a collection of urllib handlers. 134 @return: A list of handlers to be installed in the opener. 135 @rtype: [Handler,...] 136 """ 137 handlers = [] 138 handlers.append(u2.ProxyHandler(self.proxy)) 139 return handlers
140
141 - def u2ver(self):
142 """ 143 Get the major/minor version of the urllib2 lib. 144 @return: The urllib2 version. 145 @rtype: float 146 """ 147 try: 148 part = u2.__version__.split('.', 1) 149 n = float('.'.join(part)) 150 return n 151 except Exception, e: 152 log.exception(e) 153 return 0
154
155 - def __deepcopy__(self, memo={}):
156 clone = self.__class__() 157 p = Unskin(self.options) 158 cp = Unskin(clone.options) 159 cp.update(p) 160 return clone
161 162
163 -class HttpAuthenticated(HttpTransport):
164 """ 165 Provides basic http authentication for servers that don't follow 166 the specified challenge / response model. This implementation 167 appends the I{Authorization} http header with base64 encoded 168 credentials on every http request. 169 """ 170
171 - def open(self, request):
172 self.addcredentials(request) 173 return HttpTransport.open(self, request)
174
175 - def send(self, request):
176 self.addcredentials(request) 177 return HttpTransport.send(self, request)
178
179 - def addcredentials(self, request):
180 credentials = self.credentials() 181 if not (None in credentials): 182 encoded = base64.encodestring(':'.join(credentials)) 183 basic = 'Basic %s' % encoded[:-1] 184 request.headers['Authorization'] = basic
185
186 - def credentials(self):
187 return (self.options.username, self.options.password)
188