views:

169

answers:

1

This is what my code looks like:

url_object = urlparse(url)
hostname = url_object.hostname
port = url_object.port
uri = url_object.path if url_object.path else '/'

ctx = SSL.Context()
if ctx.load_verify_locations(cafile='ca-bundle.crt') != 1: raise Exception("Could not load CA certificates.")
ctx.set_verify(SSL.verify_peer | SSL.verify_fail_if_no_peer_cert, depth=5)
c = httpslib.HTTPSConnection(hostname, port, ssl_context=ctx)
c.request('GET', uri)
data = c.getresponse().read()
c.close()
return data

How can I disable url redirection in this code? I am hoping there would be some way of setting this option on connection object 'c' in the above code.

Thanks in advance for the help.

A: 

httpslib.HTTPSConnection does not redirect automatically. Like Lennart says in the comment, it subclasses from httplib.HTTPConnection which does not redirect either. It is easy to test with httplib:

import httplib

c = httplib.HTTPConnection('www.heikkitoivonen.net', 80)
c.request('GET', '/deadwinter/disclaimer.html')
data = c.getresponse().read()
c.close()
print data

This prints:

<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">
<html><head>
<title>301 Moved Permanently</title>
</head><body>
<h1>Moved Permanently</h1>
<p>The document has moved <a href="http://www.heikkitoivonen.net/deadwinter/copyright.html"&gt;here&lt;/a&gt;.&lt;/p&gt;
</body></html>

You have to handle redirects yourself with http(s)lib, see for example the request function in silmut (a simple test framework I wrote a while back).

Heikki Toivonen