views:

98

answers:

1

Like the title says, my code basically does this:

set proxy, test proxy, do some cool stuff

But after the proxy is set the first time, it sticks that way, never changing. This is the failing code:

    # Pick proxy
    r = random.randint(0, len(proxies) - 1)
    proxy = proxies[r]
    print proxy

    # Setup proxy
    l_proxy_support = urllib2.ProxyHandler({"http": "http://{0}:{1}".format(*proxy)})
    l_opener = urllib2.build_opener(l_proxy_support)

    try:
        l_req = l_opener.open(
            urllib2.Request('http://[MYIPADDRESSHERE]/ip.php', None, agent)
        )
        print l_req.read()
    except urllib2.URLError:
        print "Proxy not functioning, aborting..."

    # Do cool stuff here, not really relevant

The output ends up being:

['X.Y.Z.124', '666', 'http']
[Skarlet] Your IP address is: X.Y.Z.124
['X2.Y2.Z2.190', '666', 'http']
[Skarlet] Your IP address is: X.Y.Z.124
['X3.Y3.Z3.41', '666', 'http']
[Skarlet] Your IP address is: X.Y.Z.124

I'm really puzzled right now.

Thanks for your time.

PD: the script that responds from my home IP address is just a PHP echoing $_SERVER['REMOTE_ADDR'].

+1  A: 

That does seem strange. I've always found the httplib2 module to be the easiest Python HTTP client to work with. There is an example of using httplib2 with the socks module.

Sorry, I know this isn't a specific answer to your question, but it might be a workaround to try.

John Paulett
You're right, it's not specific, but incredibly helpful.I have replaced all the urllib2 code with the httplib2 (it wasn't THAT much code to change anyways), and I have reduced the amount of HTTP handling code quite a bit, not to mention httplib2 is much nicer to the eye.Thanks.
Just to empathize - urllib and urllib2 drive me up the wall.
Andrew Dalke