简体   繁体   English

Python:urllib2多部分/表单数据和代理

[英]Python: urllib2 multipart/form-data and proxies

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. 目标 :一个脚本,该脚本循环浏览代理列表并发送发布请求,其中包含文件到我服务器上的PHP页面,然后计算交付时间。 It's a pretty useless script, but I am using it to teach myself about urllib2. 这是一个非常无用的脚本,但是我正在用它来教自己关于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. 问题 :到目前为止,我已经使用Poster正确地发送了多部分/表单数据,但是我无法通过代理发送它,更不用说代理列表了。 I have tried using an OpenerDirector with urllib2.ProxyHandler , but I believe Poster defines it's own opener to perform it's magic. 我已经使用OpenerDirector与尝试urllib2.ProxyHandler ,但我相信Poster定义它自己的揭幕战来执行它的魔力。

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. 当我陷入困境时,任何帮助将不胜感激。

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)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM