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?
You need to ask user for password. You could use input() , but that would show the password in terminal, to avoid that you should use getpass instead: import getpass user = getpass. getuser() password = getpass.
Passwords in Plain Text Files You could store username/password on the first two lines of a plain text file, then use python to read it when you need it. If the text file is in the repository directory you should modify . gitignore to ensure it's not tracked by source source control.
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)
I haven't played with python in awhile, but try this:
urllib.urlopen("http://username:[email protected]/path")
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With