Python:urllib2 multipart/form-data 和代理

发布于 2024-08-12 19:47:25 字数 868 浏览 3 评论 0原文

目标:一个脚本,循环遍历代理列表并向我的服务器上的 PHP 页面发送包含文件的 post 请求,然后计算传递时间。这是一个非常无用的脚本,但我用它来自学 urllib2。

问题:到目前为止,我已经使用 Poster< 正确发送了 multipart/form-data /a>,但我无法让它通过代理发送,更不用说代理的循环列表了。我尝试过将 OpenerDirector 与 urllib2.ProxyHandler 结合使用,但我相信 Poster 定义了它自己的开启器来发挥其魔力。

下面是发送带有海报的多部分/表单数据请求的代码。

import urllib2
from poster.encode import multipart_encode
from poster.streaminghttp import register_openers

fields = {"type": "image",
          "fileup": open("/home/chaz/pictures/test.jpg", "rb")
         }

register_openers() #I believe this is the key
datagen, headers = multipart_encode(fields)
request = urllib2.Request("http://foo.net", datagen, headers)

lastResponse = urllib2.urlopen(request).read()

任何帮助将不胜感激,因为我很困惑。

The Objective: A script which cycles through a list of proxies and sends a post request, containing a file to a PHP page on my server, which then calculates delivery time. It's a pretty useless script, but I am using it to teach myself about urllib2.

The Problem: So far I have got multipart/form-data sending correctly using Poster, but I can't get it to send through a proxy, let alone a cycling list of proxies. I have tried using an OpenerDirector with urllib2.ProxyHandler, but I believe Poster defines it's own opener to perform it's magic.

Below is the code to send a multipart/form-data request with poster.

import urllib2
from poster.encode import multipart_encode
from poster.streaminghttp import register_openers

fields = {"type": "image",
          "fileup": open("/home/chaz/pictures/test.jpg", "rb")
         }

register_openers() #I believe this is the key
datagen, headers = multipart_encode(fields)
request = urllib2.Request("http://foo.net", datagen, headers)

lastResponse = urllib2.urlopen(request).read()

Any help would be much appreciated as I am stumped.

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

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

发布评论

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

评论(1

强辩 2024-08-19 19:47:25

您可以在请求页面之前添加这样的代理安装程序。

from urllib2 import ProxyHandler,build_opener,install_opener

PROXY="http://USERNAME:PASSWD@ADDRESS:PORT"

opener = build_opener(ProxyHandler({"http" : PROXY}))

install_opener(opener)

you could add proxy installer like this, before requesting the page.

from urllib2 import ProxyHandler,build_opener,install_opener

PROXY="http://USERNAME:PASSWD@ADDRESS:PORT"

opener = build_opener(ProxyHandler({"http" : PROXY}))

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