views:

297

answers:

1

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.

+4  A: 

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)
S.Mark
Figured it out on my own, your answer is solid advice, so enjoy the points ; )
Chazadanga