16

Messing with Python and I'm trying to use this https://updates.opendns.com/nic/update?hostname=, when you got to the URL it will prompt a username and password. I've been looking around and I found something about password managers, so I came up with this:

urll = "http://url.com"
username = "username"
password = "password"

passman = urllib2.HTTPPasswordMgrWithDefaultRealm()

passman.add_password(None, urll, username, password)

authhandler = urllib2.HTTPBasicAuthHandler(passman)

urllib2 = urllib2.build_opener(authhandler)

pagehandle = urllib.urlopen(urll)

print (pagehandle.read())

This all works, but it's prompting the username and password via the command line, requiring the user's interaction. I want it to automatically enter those values. What am I doing wrong?

0

4 Answers 4

24

You could use requests instead. The code is as simple as:

import requests
url = 'https://updates.opendns.com/nic/update?hostname='
username = 'username'
password = 'password'
print(requests.get(url, auth=(username, password)).content)
Sign up to request clarification or add additional context in comments.

2 Comments

Hi, while compiling the above snippet, I got an error: b'{"error_code":401,"error":"authentication_required","error_message":"Please log in to continue."}'
@NoobGeek You would get a 401 response if the username/password don't match what the server expects. If you used this exact example (url + credentials) I'm pretty sure you will get a 401 (since those credentials are made up for the purpose of the example).
4

Your request url is "RESTRICTED".

If you try this code it will tell you:

import urllib2
theurl = 'https://updates.opendns.com/nic/update?hostname='
req = urllib2.Request(theurl)
try:
    handle = urllib2.urlopen(req)
except IOError, e:
    if hasattr(e, 'code'):
        if e.code != 401:
            print 'We got another error'
            print e.code
        else:
            print e.headers
            print e.headers['www-authenticate']

You should add authorization headers. For more details have a look into: http://www.voidspace.org.uk/python/articles/authentication.shtml

And another code example is: http://code.activestate.com/recipes/305288-http-basic-authentication/

If you wish to send POST request , try it:

import urllib
import urllib2
username = "username"
password = "password"
url = 'http://url.com/'
values = { 'username': username,'password': password }
data = urllib.urlencode(values)
req = urllib2.Request(url, data)
response = urllib2.urlopen(req)
result = response.read()
print result

Note: this is just an example of how to send POST request to URL.

4 Comments

@AnthonyHonciano be careful username and password (dict keys) should be name of your input elements. and is your form has other field? can you paste your form code in your status? (update your status). 401 means Unauthorized.
There is no form, just that. I'm just building a personal updater app for myself. So I'll have the script run like this run I run the python script. Also yes, I know that "username and password" will need to be different the actual account info.
@AnthonyHonciano I updated my post. please check it. hope it solves your problem ;)
Perfect perfect perfecto!!! Than you, the top part with the base64 worked like a charm!
3

I haven't played with python in awhile, but try this:

urllib.urlopen("http://username:[email protected]/path")

2 Comments

Ya, python doesn't like that because of the symbols... I've tried so many methods to use that url that way.. No success
It is an insecure method to add to a script since the password is open for the reader to view. However this worked like a charm for some scripts I was running on an internal network.
0

If you like me need Digest Auth, I recommend using "requests" builtin method requests.auth.HTTPDigestAuth:

import requests

resp = requests.get("http://myserver.com/endpoint",
                    auth=requests.auth.HTTPDigestAuth("username", "password"))

1 Comment

It work for me in python 3.8.5. Do you have any idea how to download all those datasets?

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.