Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python: Urllib.urlopen nonnumeric port

for the following code

theurl = "https://%s:%[email protected]/nic/update?hostname=%s&myip=%s&wildcard=NOCHG&mx=NOCHG&backmx=NOCHG" % (username, password, hostname, theip)

conn = urlopen(theurl) # send the request to the url
print(conn.read())  # read the response
conn.close()   # close the connection

i get the following error

File "c:\Python31\lib\http\client.py", line 667, in _set_hostport
    raise InvalidURL("nonnumeric port: '%s'" % host[i+1:])

Any Ideas???

like image 353
user404288 Avatar asked Jul 28 '10 08:07

user404288


2 Answers

You probably need to url-encode the password. You'll see an error like that if the password happens to contain a '/'.

Here's a local example (actual values redacted):

>>> opener
<urllib.FancyURLopener instance at 0xb6f0e2ac>
>>> opener.open('http://admin:[email protected]')
<addinfourl at 3068618924L whose fp = <socket._fileobject object at 0xb6e7596c>>
>>> opener.open('http://admin:somepass/[email protected]')
*** InvalidURL: nonnumeric port: 'somepass'

Encode the password:

>>> opener.open('http://admin:somepass%[email protected]')

You can use urllib.quote('somepass/a', safe='') to do the encoding.

like image 179
Jean Jordaan Avatar answered Oct 26 '22 09:10

Jean Jordaan


I agree with muckabout, this is the problem. You're probably used to using this in a browser, which would cause the browser to authenticate with the host. You should probably drop everything before the first @ sign.

have a look at urllib docs, specifically FancyURLOpener which might resolve your issue with authentication.

like image 36
Yoni H Avatar answered Oct 26 '22 11:10

Yoni H