使用 M2Crypto SSL 时如何在 Python 中禁用 URL 重定向?
这就是我的代码的样子:
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?我希望有某种方法可以在上面的代码中的连接对象“c”上设置此选项。
预先感谢您的帮助。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
httpslib.HTTPSConnection 不会自动重定向。就像 Lennart 在评论中所说,它是 httplib.HTTPConnection 的子类,它也不重定向。使用 httplib 进行测试很容易:
此打印:
您必须使用 http(s)lib 自己处理重定向,请参阅 silmut (我不久前编写的一个简单的测试框架)。
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:
This prints:
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).