1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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
34 """
35 HTTP transport using urllib2. Provided basic http transport
36 that provides for cookies, proxies but no authentication.
37 """
38
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
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
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
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
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
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
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
161
162
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):
174
175 - def send(self, request):
178
185
188