使用 M2Crypto SSL 时如何在 Python 中禁用 URL 重定向?

发布于 2024-08-07 00:24:07 字数 624 浏览 8 评论 0原文

这就是我的代码的样子:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

橙幽之幻 2024-08-14 00:24:07

httpslib.HTTPSConnection 不会自动重定向。就像 Lennart 在评论中所说,它是 httplib.HTTPConnection 的子类,它也不重定向。使用 httplib 进行测试很容易:

import httplib

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

此打印:

<!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">here</a>.</p>
</body></html>

您必须使用 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:

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">here</a>.</p>
</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).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文