在 Python 中通过 urllib2 使用 cookie 和代理

发布于 2025-01-03 15:10:39 字数 294 浏览 4 评论 0原文

我正在使用 urllib2 与网络服务器交互。对于我需要解决的具体问题,我需要通过代理对流量进行隧道传输。我设法用 urllib2 'ProxyHandler' 做到了这一点。

我还需要接受和发送cookie。我设法使用 urllib2 'cookielib.LWPCookieJar()' 来做到这一点。

问题是,虽然他们单独工作,但他们不能“一起”工作。我用“urllib2.install_opener(opener)”添加的最后一个开启器是可以工作的。

是否可以有两个活跃的“开启者”?或者用另一种方法来解决这个问题?

I'm using urllib2 to interact with a webserver. For the specific problem I need to solve, I need to tunnel the traffic through a proxy. I managed to do that with a urllib2 'ProxyHandler'.

I also need to accept and send cookies. I managed to do that with a urllib2 'cookielib.LWPCookieJar()'.

The problem is that while they work individually, they don't work 'together'. The last opener that I add with 'urllib2.install_opener(opener)' is the one that will work.

Is is possible to have two active 'openers'? Or another way to solve this problem?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

诺曦 2025-01-10 15:10:39

将代理处理程序和 cookie 处理器组合在一个 opener 中:

cj = cookielib.CookieJar()
opener = build_opener(ProxyHandler({'http': 'ip:port'}), HTTPCookieProcessor(cj))

Combine proxy handler and cookie processor in a single opener:

cj = cookielib.CookieJar()
opener = build_opener(ProxyHandler({'http': 'ip:port'}), HTTPCookieProcessor(cj))
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文