Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #71514 > unrolled thread
| Started by | pratibha natani <en.pratibha@gmail.com> |
|---|---|
| First post | 2014-05-13 16:33 -0700 |
| Last post | 2014-05-15 03:03 +1000 |
| Articles | 5 — 3 participants |
Back to article view | Back to comp.lang.python
httplib with NETRC authentication pratibha natani <en.pratibha@gmail.com> - 2014-05-13 16:33 -0700
Re: httplib with NETRC authentication Chris Angelico <rosuav@gmail.com> - 2014-05-14 10:11 +1000
Re: httplib with NETRC authentication Akira Li <4kir4.1i@gmail.com> - 2014-05-14 19:44 +0400
Re: httplib with NETRC authentication Akira Li <4kir4.1i@gmail.com> - 2014-05-14 20:47 +0400
Re: httplib with NETRC authentication Chris Angelico <rosuav@gmail.com> - 2014-05-15 03:03 +1000
| From | pratibha natani <en.pratibha@gmail.com> |
|---|---|
| Date | 2014-05-13 16:33 -0700 |
| Subject | httplib with NETRC authentication |
| Message-ID | <a432c9a5-ac1e-4e83-8fb8-010e107846b3@googlegroups.com> |
Hi,
I am trying to establish http connection to a gerrit host using netrc authentication. I have a netrc file created with following entries:
machine host1.com login name password pass
I did a debug and saw that my netrc file is being read correctly. Also in the connection object(after sending request) a header got created with appropriate credentials:
'headers': {'Authorization': 'Basic QWxhZGRpbjpvcGVuIHNlc2FtZQ='}
Still I get 401,Unauthorized in response. Any help would be greatly appreciated!
headers = headers or {}
bare_host = host.partition(':')[0]
auth = NETRC.authenticators(bare_host)
if auth:
headers.setdefault('Authorization', 'Basic %s' % (
base64.b64encode('%s:%s' % (auth[0], auth[2]))))
print "inside auth 0 ", auth[0]
print "inside auth 2 ", auth [2]
if body:
body = json.JSONEncoder().encode(body)
headers.setdefault('Content-Type', 'application/json')
conn = httplib.HTTPSConnection(host)
conn.req_host = host
conn.req_params = {
'url': '/a/%s' % path,
'method': reqtype,
'headers': headers,
'body': body,
}
conn.request(**conn.req_params)
print "conn ", conn.__dict__
Thanks
Pratibha
[toc] | [next] | [standalone]
| From | Chris Angelico <rosuav@gmail.com> |
|---|---|
| Date | 2014-05-14 10:11 +1000 |
| Message-ID | <mailman.9984.1400026287.18130.python-list@python.org> |
| In reply to | #71514 |
On Wed, May 14, 2014 at 9:33 AM, pratibha natani <en.pratibha@gmail.com> wrote:
> I am trying to establish http connection to a gerrit host using netrc authentication. I have a netrc file created with following entries:
> machine host1.com login name password pass
>
> I did a debug and saw that my netrc file is being read correctly. Also in the connection object(after sending request) a header got created with appropriate credentials:
> 'headers': {'Authorization': 'Basic QWxhZGRpbjpvcGVuIHNlc2FtZQ='}
>
> Still I get 401,Unauthorized in response. Any help would be greatly appreciated!
The obvious question is: What *does* work? Does it work when you use
wget, or some other application? Then go and look at what that sends
for its authentication headers. Tip: It won't be "Authorization".
ChrisA
[toc] | [prev] | [next] | [standalone]
| From | Akira Li <4kir4.1i@gmail.com> |
|---|---|
| Date | 2014-05-14 19:44 +0400 |
| Message-ID | <mailman.10011.1400082607.18130.python-list@python.org> |
| In reply to | #71514 |
Chris Angelico <rosuav@gmail.com> writes:
> On Wed, May 14, 2014 at 9:33 AM, pratibha natani <en.pratibha@gmail.com> wrote:
>> I am trying to establish http connection to a gerrit host using
>> netrc authentication. I have a netrc file created with following
>> entries:
>> machine host1.com login name password pass
>>
>> I did a debug and saw that my netrc file is being read
>> correctly. Also in the connection object(after sending request) a
>> header got created with appropriate credentials:
>> 'headers': {'Authorization': 'Basic QWxhZGRpbjpvcGVuIHNlc2FtZQ='}
>>
>> Still I get 401,Unauthorized in response. Any help would be greatly appreciated!
>
> The obvious question is: What *does* work? Does it work when you use
> wget, or some other application? Then go and look at what that sends
> for its authentication headers. Tip: It won't be "Authorization".
"Authorization" is the appropriate header for basic http authentication:
http://tools.ietf.org/html/rfc2617#section-2
Here's a code example for urllib2:
https://gist.github.com/kennethreitz/973705#comment-56387
--
akira
[toc] | [prev] | [next] | [standalone]
| From | Akira Li <4kir4.1i@gmail.com> |
|---|---|
| Date | 2014-05-14 20:47 +0400 |
| Message-ID | <mailman.10014.1400086056.18130.python-list@python.org> |
| In reply to | #71514 |
pratibha natani <en.pratibha@gmail.com> writes:
> Hi,
>
> I am trying to establish http connection to a gerrit host using netrc authentication. I have a netrc file created with following entries:
> machine host1.com login name password pass
>
> I did a debug and saw that my netrc file is being read correctly. Also in the connection object(after sending request) a header got created with appropriate credentials:
> 'headers': {'Authorization': 'Basic QWxhZGRpbjpvcGVuIHNlc2FtZQ='}
>
It should be QWxhZGRpbjpvcGVuIHNlc2FtZQ== (note: the second '=')
Also, make sure that you use the correct case for the username. Userids
might be case sensitive.
--
akira
[toc] | [prev] | [next] | [standalone]
| From | Chris Angelico <rosuav@gmail.com> |
|---|---|
| Date | 2014-05-15 03:03 +1000 |
| Message-ID | <mailman.10015.1400086998.18130.python-list@python.org> |
| In reply to | #71514 |
On Thu, May 15, 2014 at 1:44 AM, Akira Li <4kir4.1i@gmail.com> wrote: >> The obvious question is: What *does* work? Does it work when you use >> wget, or some other application? Then go and look at what that sends >> for its authentication headers. Tip: It won't be "Authorization". > > "Authorization" is the appropriate header for basic http authentication: > http://tools.ietf.org/html/rfc2617#section-2 *facepalm* Critical reading failure on my part. He did have it right in the first place. Still, I stand by the original statement and (more importantly) the original question. It's just the tip that's wrong. ChrisA
[toc] | [prev] | [standalone]
Back to top | Article view | comp.lang.python
csiph-web