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

Source Code for Module httxlib.httxs

  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  Reimplementation of HTTP/HTTPSConnection to improve usability and enable 
 33  certificate validation 
 34  ''' 
 35   
 36  from httplib import HTTPConnection 
 37  import socket 
 38   
39 -class HTTPxTunneled(HTTPConnection):
40 ''' 41 Subclass of HTTPSxConnection that will use an existing connected sock 42 ''' 43
44 - def __init__(self, sock, host, port=None, strict=None, timeout=socket._GLOBAL_DEFAULT_TIMEOUT):
45 ''' 46 Constructor. It delegates construction to the base class 47 HTTPConnection and initializes the new member variables 48 with the default values from the Python documentation 49 50 @param sock: connected sock to use 51 @type sock: socket 52 @param host: host to connect to 53 @type host: str 54 @param port: port to connect to. use None for the default HTTP port 55 @type port: int|None 56 @param strict: n/a 57 @type strict: Unknown 58 @param timeout: default time for network operations 59 @type timeout: float 60 ''' 61 HTTPConnection.__init__(self, host, port, strict, timeout) 62 self.sock = sock
63 64
65 - def connect(self):
66 ''' 67 Overrriding is needed to avoid establishing a new connection ... this goes over 68 an existing socket 69 ''' 70 pass
71 72 73 try: 74 import ssl 75 except ImportError:
76 - class HTTPSxConnection(object):
77 ''' 78 Stub class to raise an exception on instantiation and let the user know that SSL 79 failed 80 '''
81 - def __init__(self, **kwargs):
82 ''' 83 Constructor. It simply raises an exception 84 @raise NotImplemented 85 ''' 86 raise NotImplemented('SSL support is missing. Https connections cannot be opened')
87 88
89 - class HTTPSxTunneled(object):
90 ''' 91 Stub class to raise an exception on instantiation and let the user know that SSL 92 failed 93 '''
94 - def __init__(self, **kwargs):
95 ''' 96 Constructor. It simply raises an exception 97 @raise NotImplemented 98 ''' 99 raise NotImplemented('SSL support is missing. Https connections cannot be opened')
100 101 else: 102 from httplib import HTTPSConnection 103
104 - class HTTPSxConnection(HTTPSConnection):
105 ''' 106 Reimplementation of HTTPSConnection but sublcassing from L{HTTPSConnection} to allow 107 certificate validation if wished 108 109 HTTPxConnection omitted this possibilitiy which is supported by the 110 Python ssl library and the wrap_socket functionality 111 112 HTTPxConnection is subclassed, receives two new member variables 113 and overrides connect to better call ssl.wrap_socket 114 115 Please read the Python 2.6 documentation on SSL and certificate validation 116 117 @ivar cert_reqs: It dictates if certificate validation will be done. 118 It uses the ssl: CERT_NONE, CERT_OPTIONAL, CERT_REQUIRED 119 values from the ssl module 120 @type cert_reqs: enumeration 121 @ivar ca_certs: Path to the file containing the root (chain of) 122 certificate(s) 123 @type ca_certs: str 124 ''' 125
126 - def __init__(self, host, port=None, key_file=None, cert_file=None, 127 strict=None, timeout=socket._GLOBAL_DEFAULT_TIMEOUT):
128 ''' 129 Constructor. It delegates construction to the base class 130 HTTPConnection and initializes the new member variables 131 with the default values from the Python documentation 132 133 @param host: host to connect to 134 @type host: str 135 @param port: port to connect to. use None for the default HTTP port 136 @type port: int|None 137 @param key_file: path to private key_file or None if stored in cert_file 138 @type key_file: str|None 139 @param cert_file: path to certificate to be used for validation or None 140 @type cert_file: str|None 141 @param strict: n/a 142 @type strict: Unknown 143 @param timeout: default time for network operations 144 @type timeout: float 145 ''' 146 HTTPSConnection.__init__(self, host, port, key_file, cert_file, strict, timeout) 147 self.cert_reqs = ssl.CERT_NONE 148 self.ca_certs = None
149 150
151 - def connect(self):
152 ''' 153 Opens the connection and wraps it in a ssl socket 154 155 Overrriding is needed to enable certificate validation 156 ''' 157 self.sock = socket.create_connection((self.host, self.port), 158 self.timeout, self.source_address) 159 self.sslwrap()
160 161
162 - def sslwrap(self):
163 ''' 164 Wraps the connection into a ssl socket 165 ''' 166 self.sock = ssl.wrap_socket(self.sock, 167 self.key_file, self.cert_file, 168 cert_reqs=self.cert_reqs, ca_certs=self.ca_certs)
169 170
171 - class HTTPSxTunneled(HTTPSxConnection):
172 ''' 173 Subclass of HTTPSxConnection that will use an existing connected sock 174 ''' 175
176 - def __init__(self, sock, host, port=None, key_file=None, cert_file=None, 177 strict=None, timeout=socket._GLOBAL_DEFAULT_TIMEOUT):
178 ''' 179 Constructor. It delegates construction to the base class 180 HTTPConnection and initializes the new member variables 181 with the default values from the Python documentation 182 183 HTTPConnection and initializes the new member variables 184 with the default values from the Python documentation 185 186 @param sock: connected sock to use 187 @type sock: socket 188 @param host: host to connect to 189 @type host: str 190 @param port: port to connect to. use None for the default HTTP port 191 @type port: int|None 192 @param key_file: path to private key_file or None if stored in cert_file 193 @type key_file: str|None 194 @param cert_file: path to certificate to be used for validation or None 195 @type cert_file: str|None 196 @param strict: n/a 197 @type strict: Unknown 198 @param timeout: default time for network operations 199 @type timeout: float 200 ''' 201 HTTPSxConnection.__init__(self, host, port, key_file, cert_file, strict, timeout) 202 self.sock = sock
203 204
205 - def connect(self):
206 ''' 207 Wraps the existing socket into SSL 208 ''' 209 self.sslwrap()
210