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:
En开发者_如何转开发ter 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
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)
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.
I adapted Denis' code a little to this:
import urllib
urllib.FancyURLopener.prompt_user_passwd = lambda *a, **k: (None, None)
It will disable password prompting altogether
精彩评论