0

I'm trying to make script that check for the web page if it requires a basic http authentication or not before executing the needed command .

I'm sorry but i don't understand the libraries and the commands related to this check at python , I tried to search for it but failed to find any useful info .

For example I need the script to check the page www.google.com if it is gonna ask for credentials to view the page or not then complete the command.

1
  • 1
    Anything missing from the answer to make you un-accept a year later? :-) Commented May 6, 2015 at 6:59

1 Answer 1

3

If a server expects the client to use basic authentication, it'll respond to a request without such authentication with a WWW-Authenticate header, containing the word 'Basic'. See the Basic Authentication Scheme section of the HTTP RFC.

Using the standard Python libraries, you can test for that with:

from urllib2 import urlopen, HTTPError

try:
    response = urlopen(url)
except HTTPError as exc:
    # A 401 unauthorized will raise an exception
    response = exc
auth = response.info().getheader('WWW-Authenticate')
if auth and auth.lower().startswith('basic'):
    print "Requesting {} requires basic authentication".format(url)

Demo:

>>> from urllib2 import urlopen, HTTPError
>>> url = 'http://httpbin.org/basic-auth/user/passwd'
>>> try:
...     response = urlopen(url)
... except HTTPError as exc:
...     # A 401 unauthorized will raise an exception
...     response = exc
... 
>>> auth = response.info().getheader('WWW-Authenticate')
>>> if auth and auth.lower().startswith('basic'):
...     print "Requesting {} requires basic authentication".format(url)
... 
Requesting http://httpbin.org/basic-auth/user/passwd requires basic authentication

To add a timeout to the request as well, use:

from urllib2 import urlopen, HTTPError
import socket


response = None

try:
    response = urlopen(url, timeout=15)
except HTTPError as exc:
    # A 401 unauthorized will raise an exception
    response = exc
except socket.timeout:
    print "Request timed out"

auth = response and response.info().getheader('WWW-Authenticate')
if auth and auth.lower().startswith('basic'):
    print "Requesting {} requires basic authentication".format(url)
Sign up to request clarification or add additional context in comments.

7 Comments

Thanks, dude it worked , but could you explain where i can add a timeout for the code ?
@abualameer94: Use urlopen(url, timeout=15) for a timeout of 15 seconds.
Thanks, Martijn . I added the timeout and it worked perfectly but when the url exceed the time limit it shows it as and error how i can add an exception for the timeout issue .?
@abualameer94: the timeout throws a socket.timeout exception; you'll have to import that and handle it separately.
Hello, After editing the code to the code you gave me I faced this error , It's a strange one . the previous one was working perfectly and now both is not working ..
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.