views:

135

answers:

2

Hello, I'm using urllib.urlopen with some http proxies and sometimes (probably when they require authorization) I get the following prompt printed into the console:

Enter username for Private Proxy Access (country) at xxx.xxx.xxx.xxx:xxxx

How can I raise an exception on such thing happening?

Here's the example:

from urllib import urlopen

p = '64.79.209.238:36867'
print urlopen('http://google.com', proxies={'http': 'http://'+p})

In case the mentioned proxy dies too soon, here are some replacements 64.79.197.36:43444, 64.79.209.203:34968, 64.79.197.36:43444, 209.59.207.197:3438

+1  A: 

urllib.urlencode doesn't ever print that message. That method never hits the network in first place. Your problem is somewhere else.

I guess you're running some other daemon in background that prints the message.

Please provide exact reproducible code and message. Example of proxy that triggers the message would also be good.

nosklo
Added it to the OP
roddik
And sorry, I've only now noticed that I've written urlencode instead of urlopen
roddik
+1  A: 

You need to overwrite FancyURLopener.prompt_user_passwd method:

class AuthorizationRequired(Exception):

    pass


class MyURLOpener(urllib.FancyURLopener):

    def prompt_user_passwd(self, host, realm):
        raise AuthorizationRequired()


opener = MyURLOpener(proxies={'http': 'http://'+p})
fp = opener.open(url)
Denis Otkidach