By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,402 Members | 1,047 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,402 IT Pros & Developers. It's quick & easy.

httplib.InvalidURL: nonnumeric port: For characters in the proxy password in URL

P: n/a
Hi,
The following piece of code works properly when my proxy password
contains characters[a-zA-Z0-9] etc. But when my proxy password
contained something like '|\/|' , the httplib incorrectly indentified
it as separator. How do I resolve this issue.

<code>
# Proxy Address
PROXY_IP = "1.1.9.8:80"

# Trying with linear way

proxy_user = 'user_name'
proxy_password='|\/|something'

# Setup the Proxy with urllib2

proxy_url = 'http://' + proxy_user + ':' + proxy_password + '@' +
PROXY_IP
proxy_support = urllib2.ProxyHandler({"http":proxy_url})
opener = urllib2.build_opener(proxy_support,urllib2.HTTPHan dler)
urllib2.install_opener(opener)

</code>

I get the Error:

Traceback (most recent call last):
File "C:\Python24\forge\ngwallp\ngwall.py", line 35, in ?
data = urllib2.urlopen(site)
File "C:\Python24\lib\urllib2.py", line 130, in urlopen
return _opener.open(url, data)
File "C:\Python24\lib\urllib2.py", line 358, in open
response = self._open(req, data)
File "C:\Python24\lib\urllib2.py", line 376, in _open
'_open', req)
File "C:\Python24\lib\urllib2.py", line 337, in _call_chain
result = func(*args)
File "C:\Python24\lib\urllib2.py", line 1021, in http_open
return self.do_open(httplib.HTTPConnection, req)
File "C:\Python24\lib\urllib2.py", line 980, in do_open
h = http_class(host) # will parse host:port
File "C:\Python24\lib\httplib.py", line 586, in __init__
self._set_hostport(host, port)
File "C:\Python24\lib\httplib.py", line 598, in _set_hostport
raise InvalidURL("nonnumeric port: '%s'" % host[i+1:])
httplib.InvalidURL: nonnumeric port: '|\'
Thanks,
Senthil

Nov 13 '06 #1
Share this Question
Share on Google+
11 Replies


P: n/a
Phoe6 wrote:
Hi,
The following piece of code works properly when my proxy password
contains characters[a-zA-Z0-9] etc. But when my proxy password
contained something like '|\/|' , the httplib incorrectly indentified
it as separator.
if you want to prevent the URI parser from treating something as part of
the URI, you need to quote it:

pwd = urllib.quote(pwd, "")

</F>

Nov 13 '06 #2

P: n/a
Fredrik Lundh wrote:
Phoe6 wrote:
Hi,
The following piece of code works properly when my proxy password
contains characters[a-zA-Z0-9] etc. But when my proxy password
contained something like '|\/|' , the httplib incorrectly indentified
it as separator.

if you want to prevent the URI parser from treating something as part of
the URI, you need to quote it:

pwd = urllib.quote(pwd, "")
Tried this and did not work out. It ended up confusing urllib and
urllib2 objects.
I dont want to import urllib, with the urllib2 itself wanted to work
around this piece of code.

<code>
proxy_user = r'senthil_or'
proxy_password='|\/|pr0c'
#proxy_password = r'|\/|pr0c'

# Setup the Proxy with urllib2

proxy_url = r'http://' + proxy_user + ':' + proxy_password + '@' +
PROXY_IP

proxy_support = urllib2.ProxyHandler({"http":proxy_url})
opener = urllib2.build_opener(proxy_support,urllib2.HTTPHan dler)
urllib2.install_opener(opener)
</code>

Thanks,
Senthil

Nov 13 '06 #3

P: n/a
"Phoe6" wrote:
>if you want to prevent the URI parser from treating something as part of
the URI, you need to quote it:

pwd = urllib.quote(pwd, "")

Tried this and did not work out. It ended up confusing urllib and
urllib2 objects.
oh, please. urllib.quote is a *function*; there's no way that calling that function
from code written for urllib2 will affect anything.

but you can access urllib.quote from the urllib2 namespace too. it's actually the
very same function:
>>urllib.quote
<function quote at 0x00F430F0>
>>urllib2.quote
<function quote at 0x00F430F0>

(urllib2 imports lots of stuff from urllib)
I dont want to import urllib, with the urllib2 itself wanted to work
around this piece of code.
not wanting to solve a problem is a pretty lousy problem-solving strategy, really.

</F>

Nov 13 '06 #4

P: n/a
Fredrik Lundh wrote:

Hi Fredrik,
I apologize if I offended you or have shown any
impatience. I shall try again:
oh, please. urllib.quote is a *function*; there's no way that calling that function
from code written for urllib2 will affect anything.

but you can access urllib.quote from the urllib2 namespace too. it's actually the
very same function:
>urllib.quote
<function quote at 0x00F430F0>
>urllib2.quote
<function quote at 0x00F430F0>

(urllib2 imports lots of stuff from urllib)
I dont know, it does not for me.
>>urladd = "http://example.net:80@username:|\/|pr0c"
print urladd
http://example.net:80@username:|\/|pr0c
>>>import urllib2
url = urllib2.quote(urladd)
Traceback (most recent call last):
File "<interactive input>", line 1, in ?
AttributeError: 'module' object has no attribute 'quote'
>>import urllib
url = urllib.quote(urladd)
print url
http%3A//example.net%3A80%40username%3A%7C%5C/%7Cpr0c
>>>
I dont want to import urllib, with the urllib2 itself wanted to work
around this piece of code.

not wanting to solve a problem is a pretty lousy problem-solving strategy, really.
Sorry. :-) I shall take your advice and try to get to bottom of this
issue.
- To the piece of code, I import urllib
- use urllib.quote() to covert the proxy url to a quoted one.
and try again, I get an error:

<error>
Traceback (most recent call last):
File "C:\Python24\forge\ngwallp\ngwall.py", line 38, in ?
data = urllib2.urlopen(site)
File "C:\Python24\lib\urllib2.py", line 130, in urlopen
http://lava.nationalgeographic.com/c...nth=11&year=06
return _opener.open(url, data)
File "C:\Python24\lib\urllib2.py", line 358, in open
response = self._open(req, data)
File "C:\Python24\lib\urllib2.py", line 376, in _open
'_open', req)
File "C:\Python24\lib\urllib2.py", line 337, in _call_chain
result = func(*args)
File "C:\Python24\lib\urllib2.py", line 573, in <lambda>
lambda r, proxy=url, type=type, meth=self.proxy_open: \
File "C:\Python24\lib\urllib2.py", line 580, in proxy_open
if '@' in host:
TypeError: iterable argument required
</error>

This is where, I gave up and wrote the previous email as not to mess up
urllib with urllib2.
The piece of code, was working properly till I changed my proxy
password to something containng '|\/|'.
- Before shooting a mail to the grooups, i kindda encoded password to
valid url characters ( %7C%5C/%7C) and tried. But this did not work.
So, I dont think this is urllib.quote() issue. I am looking for a way
as how to use the ProxyHandler with authentication in different way
than I have used below.

proxy_url_add = r'http://' + proxy_user + ':' + proxy_password + '@' +
PROXY_IP
proxy_url = urllib.quote(proxy_url_add)

proxy_support = urllib2.ProxyHandler({"http":proxy_url})
opener = urllib2.build_opener(proxy_support,urllib2.HTTPHan dler)
urllib2.install_opener(opener)

Thanks for your response.
Regards,
Senthil

Nov 13 '06 #5

P: n/a
"Phoe6" wrote:
- use urllib.quote() to covert the proxy url to a quoted one.
you should use quote to convert the *password* to quoted form, not use it on
the entire URL.

</F>

Nov 13 '06 #6

P: n/a
Fredrik Lundh wrote:
"Phoe6" wrote:
- use urllib.quote() to covert the proxy url to a quoted one.

you should use quote to convert the *password* to quoted form, not use it on
the entire URL.
Am sorry Fred. The same problem:

File "C:\Python24\lib\httplib.py", line 598, in _set_hostport
raise InvalidURL("nonnumeric port: '%s'" % host[i+1:])
httplib.InvalidURL: nonnumeric port: '|\'

I dont think its an issue to be resolved with quote().

Thanks,
Senthil

Nov 13 '06 #7

P: n/a
"Phoe6" wrote:
Am sorry Fred. The same problem:

File "C:\Python24\lib\httplib.py", line 598, in _set_hostport
raise InvalidURL("nonnumeric port: '%s'" % host[i+1:])
httplib.InvalidURL: nonnumeric port: '|\'

I dont think its an issue to be resolved with quote().
can you post the code you're using to build the URI ?

</F>

Nov 13 '06 #8

P: n/a
Fredrik Lundh wrote:
>
can you post the code you're using to build the URI ?
Okay. This piece of code fetches a page from a particular site. As I am
behind a firewall, I have to communicate through a proxy.

# Set the Proxy Address
PROXY_IP = "10.1.9.4:80"

# Trying with linear way

proxy_user = 'user_name'
proxy_password_orig='|\/|pc'
proxy_password = urllib.quote(proxy_password_orig)

#proxy_password = r'|\/|pr0c'

# Setup the Proxy with urllib2

proxy_url = 'http://' + proxy_user + ':' + proxy_password + '@' +
PROXY_IP

proxy_support = urllib2.ProxyHandler({"http":proxy_url})
opener = urllib2.build_opener(proxy_support,urllib2.HTTPHan dler)
urllib2.install_opener(opener)

# Read the ngsite
tdate = str(datetime.date.today())
sepdate = re.compile('\d\d(\d\d)-(\d+)-(\d+)')

year = sepdate.match(tdate).group(1)
month = sepdate.match(tdate).group(2)
day = sepdate.match(tdate).group(3)

site =
r"http://lava.nationalgeographic.com/cgi-bin/pod/wallpaper.cgi?day=" +
day + "&month=" + month + "&year=" + year
print site
data = urllib2.urlopen(site)

--
Senthil

Nov 13 '06 #9

P: n/a
"Phoe6" wrote:
proxy_password = urllib.quote(proxy_password_orig)
make that:

proxy_password = urllib.quote(proxy_password_orig, "")

</F>

Nov 13 '06 #10

P: n/a
Fredrik Lundh <fr*****@pythonware.comwrote:
>>>urllib.quote
<function quote at 0x00F430F0>
>>>urllib2.quote
<function quote at 0x00F430F0>
>>urllib.quote
<function quote at 0x41cb9d84>
>>urllib2.quote
Traceback (most recent call last):
File "<stdin>", line 1, in ?
AttributeError: 'module' object has no attribute 'quote'
>>sys.version
'2.4.1 (#2, May 5 2005, 11:32:06) \n[GCC 3.3.5 (Debian 1:3.3.5-12)]'

--
\S -- si***@chiark.greenend.org.uk -- http://www.chaos.org.uk/~sion/
___ | "Frankly I have no feelings towards penguins one way or the other"
\X/ | -- Arthur C. Clarke
her nu becomež se bera eadward ofdun hlęddre heafdes bęce bump bump bump
Nov 13 '06 #11

P: n/a

Fredrik Lundh wrote:
"Phoe6" wrote:
proxy_password = urllib.quote(proxy_password_orig)

make that:

proxy_password = urllib.quote(proxy_password_orig, "")
Oh yeah. Wonderful!!
That worked. Thanks a lot Fredrik!. :-) (<^prostrations)

Am sorry for the trouble and my mistake in not getting it properly the
first time. :(

Regards,
Senthil

Nov 13 '06 #12

This discussion thread is closed

Replies have been disabled for this discussion.