Package httxlib :: Module httxbase
[hide private]
[frames] | no frames]

Source Code for Module httxlib.httxbase

  1  #!/usr/bin/env python 
  2  # -*- coding: latin-1; py-indent-offset:4 -*- 
  3  ################################################################################ 
  4  #  
  5  # This file is part of HttxLib 
  6  # 
  7  # HttxLib is an HTTP(s) Python library suited multithreaded/multidomain 
  8  # applications 
  9  # 
 10  # Copyright (C) 2010-2011 Daniel Rodriguez (aka Daniel Rodriksson) 
 11  # Copyright (C) 2011 Sensible Odds Ltd 
 12  # 
 13  # You can learn more and contact the author at: 
 14  # 
 15  #    http://code.google.com/p/httxlib/ 
 16  # 
 17  # HttxLib is free software: you can redistribute it and/or modify 
 18  # it under the terms of the GNU General Public License as published by 
 19  # the Free Software Foundation, either version 3 of the License, or 
 20  # (at your option) any later version. 
 21  # 
 22  # HttxLib is distributed in the hope that it will be useful, 
 23  # but WITHOUT ANY WARRANTY; without even the implied warranty of 
 24  # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the 
 25  # GNU General Public License for more details. 
 26  # 
 27  # You should have received a copy of the GNU General Public License 
 28  # along with HttxLib. If not, see <http://www.gnu.org/licenses/>. 
 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   
40 -class HttxBase(HttxObject):
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 '''
48 - def __init__(self, **kwargs):
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
61 - def setoptions(self, **kwargs):
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
71 - def setuseragent(self, useragent):
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
81 - def setdecompmethods(self, decompmethods):
82 ''' 83 Helper method to set the decompression methods 84 85 @param decompmethods: desired decompression methods 86 @type decompmethods: list 87 ''' 88 self.options.update(decompmethods=HttxCompressionSet(*decompmethods))
89 90
91 - def add_password(self, realm, url, username, password):
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
107 - def add_certkey(self, url, certfile, keyfile):
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
125 - def add_cert_req(self, url, cert_req):
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
140 - def add_ca_cert(self, url, ca_cert):
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
157 - def setproxy(self, proxy):
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
171 - def request(self, httxreq):
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
185 - def getresponse(self, sock):
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
199 - def urlopen(self, httxreq):
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 # getresponse may have returned but still be active because a 218 # redirection or authentication request may be pending 219 if not response.isactive(): 220 break 221 222 # Used the sock inside the response, since it may be different 223 sock = response.sock 224 225 return response
226