Python httplib.InvalidURL: nonnumeric port fail
python, python-2.7, urllib2
Solution
Use BasicAuthHandler for providing the password instead:
import urllib2
passman = urllib2.HTTPPasswordMgrWithDefaultRealm()
passman.add_password(None, "http://casfcddb.xxx.com", "char_user", "char_pwd")
auth_handler = urllib2.HTTPBasicAuthHandler(passman)
opener = urllib2.build_opener(auth_handler)
urllib2.install_opener(opener)
urllib2.urlopen("http://casfcddb.xxx.com")
or using the requests library:
import requests
requests.get("http://casfcddb.xxx.com", auth=('char_user', 'char_pwd'))
Problem
I'm trying to open a URL in Python that needs username and password. My specific implementation looks like this: ``` http://char_user:char_pwd@casfcddb.example.com/...... ``` I get the following error spit to the console: ``` httplib.InvalidURL: nonnumeric port: 'char_pwd@casfcddb.example.com' ``` I'm using urllib2.urlopen, but the error is implying it doesn't understand the user credentials. That it sees the ":" and expects a port number rather than the password and actual address. Any ideas what I'm doing wrong here?