[Tutor] urllib2 issue getting realm
Sander Sweers
sander.sweers at gmail.com
Wed Aug 31 15:03:28 CEST 2011
On 31 August 2011 14:27, Johan Geldenhuys <johan at accesstel.com.au> wrote:
> On 31/08/2011, at 22:09, Sander Sweers <sander.sweers at gmail.com> wrote:
>> On 31 August 2011 09:46, Johan Geldenhuys <johan at accesstel.com.au> wrote:
>>> I am trying to use a very simple piece of code to get the realm from
>>> different HTTPS URLs.
>>>
>>> This realm is essential for successful authentication on the HTTPS session.
>>
>> I think you got 2 things mixed up, http authentication and encrypted
>> http (https). Just having a https url does not mean you need to
>> authenticate. Take 'https://mail.google.com' as an example.
>>
>>> If have to run this for a few different hosts and capture the realm.
>>> For one host, it works perfectly, but when I try the same code to get the
>>> next realm, it fails. I suspect that I need to close some type of
>>> connections or something, but I can't put my finger on it.
>> <snip>
>>
>>> For the next one:
>>>
>>>>>> try:
>>> ... req = urllib2.Request(URL % ips[1])
>>> ... handle = urllib2.urlopen(req)
>>> ... except IOError, e:
>>> ... print `e`
>>> ...
>>>>>>
>>>
>>> See, no "e" printed which means that something worked. The exception didn't
>>> happen and I don't know why...Anybody?
>>
>> Yes, when you type the url into your browser. I suspect it does not
>> ask you to authenticate via http authentication. Therefor no exception
>> is raised and you never print the headers.
Top posting is evil ;-).
> Focusing on the code below, do you know why it would raise the exception for the first
> IP and not for the second?
Run this and you should see which error is being raise. Do note we
catch urllib2.HTTPError instead of IOError.
try:
req = urllib2.Request(URL % ips[1])
handle = urllib2.urlopen(req)
print 'Success'
print handle.getcode()
print handle.headers
except urllib2.HTTPError as e:
print 'Exception caught'
print e
print e.getcode()
print e.hdrs
Greets
Sander
More information about the Tutor
mailing list