urllib.urlopen and https question

B

bowman.joseph

Hi,

I'm new to python. I've been handed the job of modifying a script we
have here at work that pulls content from a zope site, to create static
html. They wanted a check to make sure the database is up, while
pulling, to avoid errors.

I got it pretty much working how I want without any problems. It's
actually an error check I'm having a problem with.

Usernames and passwords change.... I'm using urlib.urlopen to open a
connection to a https url using the format of
https://username:password@url. This works great. However, if I put in
the wrong username, the python script prompts for user validation,
rather than giving an error.

The script is not interactive, will be run by cron. So I can't just let
it hang. I'm trying to figure out if there is a way to catch that
prompt and error out within the python script, rather that writing an
expect wrapper for it. The less interpeters I have to use, the better
for my cpu.
 
R

richmoore44

From the docs for urllib:

When performing basic authentication, a FancyURLopener instance calls
its prompt_user_passwd() method. The default implementation asks the
users for the required information on the controlling terminal. A
subclass may override this method to support more appropriate behavior
if needed.

Cheers

Rich.
 
B

bowman.joseph

That's what I get for scimming the documentation too quickly.

Got it, thanks!

For anyone else working on something similar, here is all I did... mind
you, this is my first experience with python.

# subclass of FancyURLopener so we can override the
prompt_user_password method
class DBCheckUrlOpener(urllib.FancyURLopener):
def prompt_user_passwd(self, host, realm):
"""3 args will be passed, so just catch them.
"""
sys.exit("Bad user authentication for db check.")
 
D

doobiz

Does anyone have an entire (simple) script for someone new to
programming and Python that will illustrate how to open an https site
and log on to it (with the script providing the username and password,
if that's possible). Thanks very much.

rpd

"Electricity is actually made up of extremely tiny particles called
electrons, that you cannot see with the naked eye unless you have been
drinking."--Dave Barry
 

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

No members online now.

Forum statistics

Threads
473,743
Messages
2,569,478
Members
44,899
Latest member
RodneyMcAu

Latest Threads

Top