urllib with x509 certs

Chris Rebert clp2 at rebertia.com
Sat Jul 4 06:27:46 EDT 2009


2009/7/4 Lacrima <Lacrima.Maxim at gmail.com>:
> On Jul 4, 11:24 am, Chris Rebert <c... at rebertia.com> wrote:
>> On Sat, Jul 4, 2009 at 1:12 AM, Lacrima<Lacrima.Ma... at gmail.com> wrote:
>> > Hello!
>>
>> > I am trying to use urllib to fetch some internet resources, using my
>> > client x509 certificate.
>> > I have divided my .p12 file into mykey.key and mycert.cer files.
>> > Then I use following approach:
>> >>>> import urllib
>> >>>> url = 'https://example.com'
>> >>>> xml = '''<request>
>> > ... <somexml>somexml</somexml>
>> > </request>'''
>> >>>> opener = urllib.URLopener(key_file = 'mykey.key', cert_file = 'mycert.cer')
>> >>>> f = opener.open(url, xml)
>>
>> > This works Ok! But every time I am asked to enter PEM pass phrase,
>> > which I specified during dividing my .p12 file.
>> > So my question... What should I do to make my code fetch any url
>> > automatically (without asking me every time to enter pass phrase)?
>> > As I understand there is impossible to specify pass phrase while
>> > constructing URLopener.
>> > So what should I do?
>>
>> Subclass FancyURLopener
>> [http://docs.python.org/library/urllib.html#urllib.FancyURLopener],
>> overriding the prompt_user_passwd() method
>> [http://docs.python.org/library/urllib.html#urllib.FancyURLopener.prom...].
>> Then use an instance of your subclass instead of URLopener.
>>
>> Cheers,
>> Chris
>> --http://blog.rebertia.com
>
> Hi Chris,
> Thanks for your quick reply.
> According to docs the return value of prompt_user_passwd() method
> should be a tuple (user, password), but there is no user when
> authenticating with certificate. So how should I use this method? This
> doesn't work:
>>>> import urllib
>>>> class MyOpener(urllib.FancyURLopener):
> ...      def prompt_user_passwd(self, host, realm):
> ...          return ('password')

Only a guess:

def prompt_user_passwd(self, host, realm):
    return ('', 'password')

Cheers,
Chris
-- 
http://blog.rebertia.com



More information about the Python-list mailing list