1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31 '''
32 Base Object for Connecting objects and primitives
33 '''
34
35 from httxcompressionset import HttxCompressionSet
36 from httxobject import HttxObject
37 from httxoptions import HttxOptions
38 from httxrequest import HttxRequest
39
41 '''
42 Base of HttxLib classes that make a connection providing the
43 definition and (in some cases) the implementation of primitives
44
45 @ivar options: The shared options for the connection(s)
46 @type options: L{HttxOptions}
47 '''
49 '''
50 Contructor
51
52 @param kwargs: keyword arguments for L{HttxOptions}
53 @see: L{HttxOptions}
54 '''
55 HttxObject.__init__(self)
56
57 self.options = kwargs.get('options', HttxOptions())
58 self.options.update(**kwargs)
59
60
62 '''
63 Sets the I{options} at any time during execution
64
65 @param kwargs: keyword arguments for L{options}
66 @see: L{HttxOptions}
67 '''
68 self.options.update(**kwargs)
69
70
72 '''
73 Helper method to set the User-Agent header
74
75 @param useragent: the user agent to fake
76 @type useragent: str
77 '''
78 self.options.update(useragent=useragent)
79
80
89
90
92 '''
93 Sets a I{username} and I{password} for a realm and base url
94 @param realm: Realm of application. It can be None
95 @type realm: str
96
97 @param url: base url for which to apply. It can be empty to match all
98 @type url: str
99 @param username: username for realm and base url
100 @type username: str
101 @param password: username for realm and base url
102 @type password: str
103 '''
104 self.options.passmanager.add_password(realm, url, username, password)
105
106
108 '''
109 Sets a I{certificate file} and (if needed) a I{private key file}
110 to be used for https authentication in the domain given in the url
111 The private key file is needed if the private key is not stored in the
112 certificate. Please check the Python SSL documentation to learn more
113
114 @param url: url (domain) for which to apply. It can be empty to match all
115 @type url: str
116 @param certfile: path to the cerfiticate file to be used
117 @type certfile: str
118 @param keyfile: path to the private key file (if not stored in the cerfiticate)
119 corresponding to the certificate
120 @type keyfile: str
121 '''
122 self.options.certkeyfile.add_certkey(url, certfile, keyfile)
123
124
126 '''
127 Sets the certificate verification level for the server certificate in the given
128 url. A (chain of) root certificate(s) has to be supplied with L{add_ca_cert}
129 Please check the Python SSL documentation
130
131 @param url: url (domain) for which to apply. It can be empty to match all
132 @type url: str
133 @param cert_req: verification level
134 @type cert_req: enumeration ssl.CERT_NONE, ssl.CERT_OPTIONAL, ssl.CERT_REQUIRED
135 @see: L{add_ca_cert}
136 '''
137 self.options.certreq.add_cert_req(url, cert_req)
138
139
141 '''
142 Sets the path to a file that contains the root (chain) certificate(s) to be used
143 if the certificate of the server pointed by url has been requested to be
144 verified with L{add_cert_req} and I{ssl.CERT_OPTIONAL} or I{ssl.CERT_REQUIRED}
145 was specified
146 Please check the Python SSL documentation
147
148 @param url: url (domain) for which to apply. It can be empty to match all
149 @type url: str
150 @param ca_cert: path to a file containing the root (chain) certificate(s)
151 @type ca_cert: str
152 @see: L{add_cert_req}
153 '''
154 self.options.cacert.add_ca_cert(url, ca_cert)
155
156
158 '''
159 Set the proxy for the HttxLib connecting object.
160 This is the abstract definition. To be implemented by connecting objects
161 The proxy can be different for http and https connections.
162
163 @param proxy: proxy servers. Dictionary with scheme:url pairs.
164 '*' as scheme stands for both http and https
165 @type proxy: dict
166 @raise NotImplemented
167 '''
168 raise NotImplemented
169
170
172 '''
173 Send the L{HttxRequest} httxreq to the specified server inside the request
174 This is the abstract definition. To be implemented by connecting objects
175
176 @param httxreq: Request or url to be executed
177 @type httxreq: L{HttxRequest} or url (string)
178 @return: sock
179 @rtype: opaque type for the caller (a Python sock)
180 @raise NotImplemented
181 '''
182 raise NotImplemented
183
184
186 '''
187 Recover a L{HttxResponse} corresponding to the sock (returned by L{request}
188 This is the abstract definition. To be implemented by connecting objects
189
190 @param sock: The opaque type returned by L{request}
191 @type sock: opaque (a Python sock)
192 @return: response
193 @rtype: L{HttxResponse} (compatible with httplib HTTPResponse)
194 @raise NotImplemented
195 '''
196 raise NotImplemented
197
198
200 '''
201 Fecth a url specified in the L{HttxRequest} httxreq object
202 and return it in the form of an L{HttxResponse}
203
204 @param httxreq: Request or url to be fetched
205 @type httxreq: L{HttxRequest} or url (string)
206 @return: response
207 @rtype: L{HttxResponse} (compatible with httplib HTTPResponse)
208 '''
209 if isinstance(httxreq, basestring):
210 httxreq = HttxRequest(httxreq)
211
212 sock = self.request(httxreq)
213
214 while True:
215 response = self.getresponse(sock)
216
217
218
219 if not response.isactive():
220 break
221
222
223 sock = response.sock
224
225 return response
226