Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python httplib.InvalidURL: nonnumeric port fail

I'm trying to open a URL in Python that needs username and password. My specific implementation looks like this:

http://char_user:[email protected]/......

I get the following error spit to the console:

httplib.InvalidURL: nonnumeric port: '[email protected]'

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?

like image 381
milnuts Avatar asked Dec 26 '22 22:12

milnuts


1 Answers

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'))
like image 186
Uku Loskit Avatar answered Jan 06 '23 08:01

Uku Loskit