Extensions to httplib: httpslib

231 the key can be changed with a call to its set_key method. Calling the update method with data to be encrypted will return the encrypted data.

9.2.3 Python Module Extensions

In addition to providing the low-level OpenSSL bindings and an object-oriented approach to OpenSSL in the high-level classes, M2Crypto also includes extensions to three of the modules that are part of Python itself. The extensions are what you might expect: SSL extensions to httplib , urllib , and xmlrpclib . The extensions to httplib and urllib simply support HTTPS. The extensions to xmlrpclib add an SSL_Transport class.

9.2.3.1 Extensions to httplib: httpslib

To use the httplib extensions, youll need to import the M2Crypto.httpslib module: from M2Crypto import httpslib You dont need to import from httplib as well. M2Cryptos httpslib exports all of httplib in addition to its own extensions. The httplib interface changed drastically in Version 2.0 of Python. httpslib accounts for this and provides different extensions depending on the version of Python that youre using. If youre using a version of Python earlier than 2.0, a single new class called HTTPS will be added. This class is a subclass of HTTP from httplib . The only detail that you need to concern yourself with is passing in an existing SSL context object to the constructor. For example, to connect to the local host on the default HTTPS port 443 using SSLv3, your code might look like this: from M2Crypto import SSL, httpslib context = SSL.Contextsslv3 https = httpslib.HTTPScontext, 127.0.0.1:443 If youre using Version 2.0 of Python or later, two new classes called HTTPSConnection and HTTPS will be added. HTTPSConnection is a subclass of HTTPConnection , and HTTPS is a subclass of HTTP . They both work similarly to their parent classes, but expect some extra information in their constructors in order to utilize SSL. All of the extra arguments are optional keyword arguments: key_file Specifies the path and filename of an RSA private key file to be used in establishing the connection. cert_file Specifies the path and filename of a certificate file to be used in establishing the connection. ssl_context Specifies an existing SSL context object. If it is omitted, a context will be created using the sslv23 protocol. 232 The HTTPSConnection class accepts all three keyword arguments. The HTTPS class will recognize only ssl_context , silently ignoring the others. The code to connect to the local host on the default HTTPS port 443 using SSLv3 might look like this: from M2Crypto import SSL, httpslib context = SSL.Contextsslv3 https = httpslib.HTTPSConnection127.0.0.1:443, ssl_context = context Its important to realize that these functions do not perform any real certificate verification, so the only real protection theyre providing is against passive eavesdropping attacks.

9.2.3.2 Extensions to urllib: m2urllib