Open a url with user and password

573 views
Skip to first unread message

Hossein Rashnoo

unread,
Dec 7, 2014, 5:48:02 AM12/7/14
to django...@googlegroups.com
I want to create a web-service for connection to sharepoint that do something like create a list and ...
So at first step because we use sharepoint with local ip i want to check if i can connect to our sharepoint portal via my server or not.
So i looking for something like : urllib2.urlopen("http://portal:8080/").read()
and i found this code for test:
import urllib.request
# Create an OpenerDirector with support for Basic HTTP Authentication...
auth_handler = urllib.request.HTTPBasicAuthHandler()
auth_handler.add_password(realm='PDQ Application',
                          uri='https://portal:8080/',
                          user='my username',
                          passwd='my password')
opener = urllib.request.build_opener(auth_handler)
# ...and install it globally so it can be used with urlopen.
urllib.request.install_opener(opener)
urllib.request.urlopen('http://portal:8080/')
But after last line i got this error:

Traceback (most recent call last):
  File "<console>", line 1, in <module>
  File "/usr/lib64/python2.6/urllib2.py", line 126, in urlopen
    return _opener.open(url, data, timeout)
  File "/usr/lib64/python2.6/urllib2.py", line 397, in open
    response = meth(req, response)
  File "/usr/lib64/python2.6/urllib2.py", line 510, in http_response
    'http', request, response, code, msg, hdrs)
  File "/usr/lib64/python2.6/urllib2.py", line 435, in error
    return self._call_chain(*args)
  File "/usr/lib64/python2.6/urllib2.py", line 369, in _call_chain
    result = func(*args)
  File "/usr/lib64/python2.6/urllib2.py", line 518, in http_error_default
    raise HTTPError(req.get_full_url(), code, msg, hdrs, fp)
HTTPError: HTTP Error 401: Unauthorized
Please help me.

Collin Anderson

unread,
Dec 9, 2014, 8:39:46 AM12/9/14
to django...@googlegroups.com
Hi,

My first guess is that it's using NTLM authentication instead of basic auth. If you curl -i http://portal:8080/ what "authenticate" headers do you get?

Also, you should consider upgrading to python 2.7 if you haven't :)

Collin

Erik Cederstrand

unread,
Dec 9, 2014, 9:27:04 AM12/9/14
to Django Users

> Den 09/12/2014 kl. 14.39 skrev Collin Anderson <cmawe...@gmail.com>:
> My first guess is that it's using NTLM authentication instead of basic auth. If you curl -i http://portal:8080/ what "authenticate" headers do you get?
>
> Also, you should consider upgrading to python 2.7 if you haven't :)

And nobody should need to look at urllib code anymore, either :-) Use requests instead, and https://github.com/requests/requests-ntlm if you need NTLN authentication.

Erik

Florian Schweikert

unread,
Dec 9, 2014, 10:59:11 AM12/9/14
to django...@googlegroups.com
On 2014-12-07 11:48, Hossein Rashnoo wrote:
> Please help me.

this is quite unrelated to django, maybe it would be better asking this
question on the python mailinglist[1]

-- Florian

[1] https://www.python.org/community/lists/

Reply all
Reply to author
Forward
0 new messages