urllib2 http authentication/redirection/cookie issue

2008-07-14 Thread Neuberger, Sheldon N.
I have a tricky situation here with auth/redirection/cookies and I
think I am messing something up with the handler.

I am trying to open a site http://foo.example.com which redirects
(status 302) to https://bar.example.com/ where the dashes
represent lots of subdirectories and parameters.  This second site,
bar.example.com, is the site which requires authorization (status 401).
Upon authorization, the second site will give a cookie or two then
redirect (status 302) to the original site which will then load because
of the cookie. 

The problem is that when I run my code, I still get an HTTP 401
exception. Any help would be much appreciated.

Code and traceback follows.

Regards,
Sheldon Neuberger


--
cj = cookielib.LWPCookieJar()
cookie_handler = urllib2.HTTPCookieProcessor(cj)

passmgr = urllib2.HTTPPasswordMgrWithDefaultRealm()
passmgr.add_password(None, 'https://bar.example.com',
   'username', 'password')
auth_handler = urllib2.HTTPBasicAuthHandler(passmgr)

opener = urllib2.build_opener(cookie_handler, auth_handler)
urllib2.install_opener(opener)

html = urllib2.urlopen('http://foo.example.com/qux/')

-

Traceback (most recent call last):
  File "dts.py", line 21, in 
html = urllib2.urlopen('http://foo.example.com/qux/')
  File "C:\Python25\lib\urllib2.py", line 124, in urlopen
return _opener.open(url, data)
  File "C:\Python25\lib\urllib2.py", line 387, in open
response = meth(req, response)
  File "C:\Python25\lib\urllib2.py", line 498, in http_response
'http', request, response, code, msg, hdrs)
  File "C:\Python25\lib\urllib2.py", line 419, in error
result = self._call_chain(*args)
  File "C:\Python25\lib\urllib2.py", line 360, in _call_chain
result = func(*args)
  File "C:\Python25\lib\urllib2.py", line 582, in http_error_302
return self.parent.open(new)
  File "C:\Python25\lib\urllib2.py", line 387, in open
response = meth(req, response)
  File "C:\Python25\lib\urllib2.py", line 498, in http_response
'http', request, response, code, msg, hdrs)
  File "C:\Python25\lib\urllib2.py", line 425, in error
return self._call_chain(*args)
  File "C:\Python25\lib\urllib2.py", line 360, in _call_chain
result = func(*args)
  File "C:\Python25\lib\urllib2.py", line 506, in http_error_default
raise HTTPError(req.get_full_url(), code, msg, hdrs, fp)
urllib2.HTTPError: HTTP Error 401: Unauthorized

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

RE: Writing a program under GNU/Linux for MS Windows.

2008-07-18 Thread Neuberger, Sheldon N.
If you're just writing Python code then it will run unmodified on his
Windows machine.

Are you planning on using other languages too?

-Original Message-
From: [EMAIL PROTECTED]
[mailto:[EMAIL PROTECTED] On Behalf
Of Levi Campbell
Sent: Friday, July 18, 2008 4:27 PM
To: python-list@python.org
Subject: Writing a program under GNU/Linux for MS Windows.

Hi, I'm trying to write a program for a friend of mine who uses
windows but I use GNU/Linux. I know you can use mingw and link to the
python dll, but is there a way to create a win32 service under Linux?
--
http://mail.python.org/mailman/listinfo/python-list
--
http://mail.python.org/mailman/listinfo/python-list


ntlm authentication

2008-07-21 Thread Neuberger, Sheldon N.
Is there any way to make urllib2 handle NTLM authentication?

Sheldon Neuberger


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

RE: Password prompt with mask

2008-08-05 Thread Neuberger, Sheldon N.
> Could I write my own if one does not exist?

Take a look at the getpass.py module. It's very short. The windows
version of the getpass function can be trivially modified to echo
something. The unix version is just as short but a bit more
complicated.

def win_getpass(prompt='Password: ', stream=None):
"""Prompt for password with echo off, using Windows getch()."""
if sys.stdin is not sys.__stdin__:
return default_getpass(prompt, stream)
import msvcrt
for c in prompt:
msvcrt.putch(c)
pw = ""
while 1:
c = msvcrt.getch()
if c == '\r' or c == '\n':
break
if c == '\003':
raise KeyboardInterrupt
if c == '\b':
pw = pw[:-1]
else:
pw = pw + c
msvcrt.putch('\r')
msvcrt.putch('\n')
return pw
--
http://mail.python.org/mailman/listinfo/python-list