OWA (Outlook Web Access) with urllib2

P

Pascal

Hello,

I want to acces my OWA (Outlook Web Acces - http Exchange interface)
server with urllib2 but, when I try, I've always a 401 http error.
Can someone help me (and us)?

Thanks.


here's my pyscript:
( Python 2.3.3 (#1, Apr 6 2004, 01:47:39) - [GCC 3.3.3 (SuSE Linux)]
on linux2 )
************************************************************************
#! /usr/bin/python


import urllib2


# set authentification
auth = urllib2.HTTPBasicAuthHandler()
auth.add_password('owa.mydomain', 'http://owa.mydomain/',
'**USERNAME**', '**PASSWORD**')


# don't use proxy
prox = urllib2.ProxyHandler({})


# installation
opener = urllib2.build_opener(prox, auth)
urllib2.install_opener(opener)


# start
url = urllib2.urlopen('http://owa.mydomain/exchange/')
print url.readlines()

************************************************************************

here's the traceback:
************************************************************************
Traceback (most recent call last):
File "./owa.py", line 22, in ?
url = urllib2.urlopen('http://owa.mydomain/exchange/')
File "/usr/lib/python2.3/urllib2.py", line 129, in urlopen
return _opener.open(url, data)
File "/usr/lib/python2.3/urllib2.py", line 326, in open
'_open', req)
File "/usr/lib/python2.3/urllib2.py", line 306, in _call_chain
result = func(*args)
File "/usr/lib/python2.3/urllib2.py", line 901, in http_open
return self.do_open(httplib.HTTP, req)
File "/usr/lib/python2.3/urllib2.py", line 895, in do_open
return self.parent.error('http', req, fp, code, msg, hdrs)
File "/usr/lib/python2.3/urllib2.py", line 352, in error
return self._call_chain(*args)
File "/usr/lib/python2.3/urllib2.py", line 306, in _call_chain
result = func(*args)
File "/usr/lib/python2.3/urllib2.py", line 412, in
http_error_default
raise HTTPError(req.get_full_url(), code, msg, hdrs, fp)
urllib2.HTTPError: HTTP Error 401: Accès refusé
************************************************************************

here's the LiveHTTPheaders/Firefox debug:
( the web page is well delivered )
************************************************************************
http://owa.mydomain/exchange/

GET /exchange/ HTTP/1.1
Host: owa.mydomain
User-Agent: Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.7)
Gecko/20040804 Firefox/0.9.3
Accept: text/xml,application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5
Accept-Language: en-us,en;q=0.5
Accept-Encoding: gzip,deflate
Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7
Keep-Alive: 300
Connection: keep-alive

HTTP/1.x 401 Accès refusé
Server: Microsoft-IIS/5.0
Date: Thu, 23 Sep 2004 08:56:03 GMT
WWW-Authenticate: Negotiate
WWW-Authenticate: NTLM
WWW-Authenticate: Basic realm="owa.mydomain"
Connection: close
Content-Length: 21
Content-Type: text/html

GET /exchange/ HTTP/1.1
Host: owa.mydomain
User-Agent: Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.7)
Gecko/20040804 Firefox/0.9.3
Accept: text/xml,application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5
Accept-Language: en-us,en;q=0.5
Accept-Encoding: gzip,deflate
Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7
Keep-Alive: 300
Connection: keep-alive
Authorization: Basic CGFYZW50CDPOC3FTEXAQKG==

HTTP/1.x 200 OK
Server: Microsoft-IIS/5.0
Date: Thu, 23 Sep 2004 08:56:15 GMT
Set-Cookie: sessionid=xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx,0x409;
path=/exchange/
Content-Type: text/html
Content-Length: 1071
MS-WebStorage: 6.0.6249
Cache-Control: no-cache

************************************************************************
 
J

John J. Lee

I want to acces my OWA (Outlook Web Acces - http Exchange interface)
server with urllib2 but, when I try, I've always a 401 http error. [...]
here's the LiveHTTPheaders/Firefox debug:
[...]

So, what does urllib2 send? Use ethereal, for example.


John
 
P

Pascal

This is the Python ~ IIS dialogue (with Ethereal / Follow TCP stream):

GET /exchange/ HTTP/1.0
Host: owa.mydomain
User-agent: Python-urllib/2.1

HTTP/1.1 401 Accès refusé
Server: Microsoft-IIS/5.0
Date: Mon, 27 Sep 2004 10:44:28 GMT
WWW-Authenticate: Negotiate
WWW-Authenticate: NTLM
WWW-Authenticate: Basic realm="owa.mydomain"
Content-Length: 21
Content-Type: text/html

Urllib2 didn't try to send authentification after http 401 error.
 
J

John J. Lee

This is the Python ~ IIS dialogue (with Ethereal / Follow TCP stream):

GET /exchange/ HTTP/1.0
Host: owa.mydomain
User-agent: Python-urllib/2.1

HTTP/1.1 401 Accès refusé
Server: Microsoft-IIS/5.0
Date: Mon, 27 Sep 2004 10:44:28 GMT
WWW-Authenticate: Negotiate
WWW-Authenticate: NTLM
WWW-Authenticate: Basic realm="owa.mydomain"
Content-Length: 21
Content-Type: text/html

Urllib2 didn't try to send authentification after http 401 error.

Well, there's your problem <0.5 wink>.

Seriously:

1. Test your code with Python 2.4a3. Quite possible that this is a
fixed bug.

2. If that doesn't work, debug urllib2.
HTTPPasswordMgr.find_user_password() looks a good place to stick
some print statements. Also, the method of
AbstractBasicAuthHandler. It's only a few lines of code!


John
 
P

Pascal

Effectively, if you look at http://python.org/2.4/NEWS.html you can
see that
urllib2 now recognizes Basic authentication even if other
authentication schemes are offered.

In LiveHTTPheaders/Firefox debug, there is
WWW-Authenticate: Negotiate
WWW-Authenticate: NTLM
WWW-Authenticate: Basic realm="owa.mydomain"
and the basic authentification is the last proposed.

So now, how can I force basic authentification with urllib2 (without
using Python 2.4a)?
 
M

Michael Foord

Effectively, if you look at http://python.org/2.4/NEWS.html you can
see that
urllib2 now recognizes Basic authentication even if other
authentication schemes are offered.

In LiveHTTPheaders/Firefox debug, there is
WWW-Authenticate: Negotiate
WWW-Authenticate: NTLM
WWW-Authenticate: Basic realm="owa.mydomain"
and the basic authentification is the last proposed.

So now, how can I force basic authentification with urllib2 (without
using Python 2.4a)?

You can do manual basic authentication.
See http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/305288

Regards,

Fuzzy

http://www.voidspace.org.uk/atlantibots/pythonutils.html
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,968
Messages
2,570,153
Members
46,699
Latest member
AnneRosen

Latest Threads

Top