473,386 Members | 1,748 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

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

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
11 12223
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
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
"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
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
"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
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
"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
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
"Phoe6" wrote:
proxy_password = urllib.quote(proxy_password_orig)
make that:

proxy_password = urllib.quote(proxy_password_orig, "")

</F>

Nov 13 '06 #10
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

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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Martin Fuzzey | last post by:
I am using xmlrpclib (based on httplib) in Python 2.3 on Mandrake Linux. When my client attempts to connect to a server using a "http://localhost:port" style URL there is a long delay before the...
2
by: Glauco | last post by:
I'm using a library based on httplib. Recently i've done a conversion for use of https with a key and certificate file. This goes perfectly :-) . Now, the problem is passing throw an http_proxy....
2
by: scummer | last post by:
Hi, I am having a problem with the httplib HTTPConnection object. While I can easily send requests that don't have any payload (ie. "get"), I encounter issues if I want to post xml data. If you...
0
by: Robert | last post by:
did you solve this problem? It seems to be still present here with py2.3.5. Robert -- From: Manish Jethani <manish.j@gmx.net> User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.0; en-US;...
4
by: michaelparkin | last post by:
Hi, Sorry to post what might seem like a trivial problem here, but its driving me mad! I have a simple https client that uses httplib to post data to a web server. When I post over http &...
0
by: Michael Oliver | last post by:
I am trying to write a Python client to access a Tomcat servlet using Tomcat Realm authentication with no success. I can use the httplib to connect to localhost port 8080 ok and post and get...
4
by: JuHui | last post by:
how to use httplib.HTTPConnection with http proxy?
1
by: mirandacascade | last post by:
I noticed the following lines from the connect() method of the HTTPConnection class within httplib: for res in socket.getaddrinfo(self.host, self.port, 0, socket.SOCK_STREAM): af, socktype,...
3
by: rhXX | last post by:
hi all, i'm using this tutorial example import httplib h = httplib.HTTP("www.python.org") h.putrequest('GET','/index.html') h.putheader('User-Agent','Lame Tutorial Code')...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.