Python:urllib2 multipart/form-data 和代理
目标:一个脚本,循环遍历代理列表并向我的服务器上的 PHP 页面发送包含文件的 post 请求,然后计算传递时间。这是一个非常无用的脚本,但我用它来自学 urllib2。
下面是发送带有海报的多部分/表单数据请求的代码。
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以在请求页面之前添加这样的代理安装程序。
you could add proxy installer like this, before requesting the page.