Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python handling username and password for URL

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?

like image 618
Anthony Honciano Avatar asked May 10 '14 04:05

Anthony Honciano


People also ask

How do I pass a python userName and password?

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.

How does python store usernames and passwords?

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.


2 Answers

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)
like image 187
Ion Scerbatiuc Avatar answered Oct 21 '22 06:10

Ion Scerbatiuc


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

urllib.urlopen("http://username:[email protected]/path")
like image 24
James Mason Avatar answered Oct 21 '22 07:10

James Mason