urllib2 Opener and Proxy/Authentication issues

2005-11-08 Thread Ray Slakinski
Hello,

I have defined a function to set an opener for urllib2, this opener
defines any proxy and http authentication that is required.

If the proxy has authencation itself and requests an authenticated file
I get a HTTP status code of 401 (Unauthorized access of the file being
requested) I do see in the headers the Proxy-authorization and the
Authorization headers being sent for the request.

Just to clarify, if I do not go through a proxy that has authentication
then I can get the file just fine.

Here is my code for the opener:

def setOpener(realm='', host='', uName=None, pWord=''):
   opener = urllib2.build_opener(urllib2.HTTPHandler)

   if len(settings.globalProxySetting) > 0:
  proxies = {'http' : settings.globalProxySetting, 'https' :
settings.globalProxySetting}
  if not uName == None:
 password_mgr = urllib2.HTTPPasswordMgrWithDefaultRealm()
 password_mgr.add_password(None, host, uName, pWord)
 auth_handler = urllib2.HTTPBasicAuthHandler(password_mgr)
 opener = urllib2.build_opener(urllib2.ProxyHandler(proxies),
auth_handler, urllib2.HTTPHandler)
  else:
 opener = urllib2.build_opener(urllib2.ProxyHandler(proxies),
urllib2.HTTPHandler)
   else:
  if not uName == None:
 password_mgr = urllib2.HTTPPasswordMgrWithDefaultRealm()
 password_mgr.add_password(None, host, uName, pWord)
 auth_handler = urllib2.HTTPBasicAuthHandler(password_mgr)
 opener = urllib2.build_opener(auth_handler)
  else:
 opener = urllib2.build_opener(opener)

   urllib2.install_opener(opener)


ps: settings.GlobalProxySetting is defined as a string, for example:

non-authenticated proxy: "http://192.168.1.1:3128";
authenticated proxy: "http://user:[EMAIL PROTECTED]:3128"

Thanks in advance, 
Ray

-- 
http://mail.python.org/mailman/listinfo/python-list


Filenames of files downloaded via urlretrieve that have been redirected

2005-05-10 Thread Ray Slakinski
I got a small issue, I am using urllib.urlretreive to download files
but in some cases I'm downloading from a CGI that is redirecting
urlretrieve to a different url.

Example:

urllib.urlretreive('http://someurl.com/files.asp?file=55',
'tempFileName.tmp')

Is there a way to know what filename files.asp is redirecting to so I
can rename tempFileName.tmp to the correct name?

Thanks in advance,
Ray

-- 
http://mail.python.org/mailman/listinfo/python-list