httplib.InvalidURL:非数字端口:用于URL中代理密码中的字符 [英] httplib.InvalidURL: nonnumeric port: For characters in the proxy password in URL

查看:236
本文介绍了httplib.InvalidURL:非数字端口:用于URL中代理密码中的字符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



当我的代理密码

包含字符[a-zA-Z0-9]等时,下面这段代码正常工作但是当我的代理密码

包含类似''| \ / |''的内容,httplib错误地识别

它作为分隔符。我如何解决这个问题。


< code>

#代理地址

PROXY_IP =" 1.1.9.8: 80


#尝试使用线性方式


proxy_user =''user_name''

proxy_password ='' | \ / |某事''


#用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>


我收到错误:


Traceback(最近一次调用最后一次):

文件C:\\ \\ python24 \ forge \ ngwallp \ ngwall.py",第35行,在?

data = urllib2.urlopen(网站)

文件C:\\ \\ _Python24 \lib \urllib2.py",行130,在urlopen

返回_opener.open(url,data)

文件C:\Python24 \lib \urllib2.py,第358行,打开

response = self._open(req,data)

文件C:\Python24 \lib \urllib2.py,第376行, _open

''_ open'',req)

文件C:\Python24 \lib \urllib2.py,337行,_call_chain

result = func(* args)

文件C:\Python24 \lib \urllib2.py,第1021行,http_open

返回self.do_open(httplib.HTTPConnection,req)

文件C:\Python24 \lib \urllib2.py,第980行,在do_open中

h = http_class(host)#将解析主机:port

文件C:\Python24 \lib \httplib.py,第586行,在__init__

self._set_hostport(主机,端口)

文件C:\Python24 \lib \ httplib.py,第598行,在_set_hostport中

raise INV alidURL(nonnumeric port:''%s'''" %host [i + 1:])

httplib.InvalidURL:非数字端口:''| \''

谢谢,

Senthil

解决方案

Phoe6写道:




当我的代理密码

包含字符[a-zA-Z0-9]等时,以下代码段正常工作。但是当我的代理密码

包含时类似''| \ / |'',httplib错误地识别

它作为分隔符。



如果你想阻止URI解析器将某些东西视为URI的一部分,你需要引用它:


pwd = urllib.quote(pwd,"")


< / F>


Fredrik Lundh写道:


Phoe6写道:


你好,

当我的代理密码

包含字符[a-zA-Z0-9]等时,下面这段代码正常工作。但是当我的代理密码
$ b时$ b包含类似''| \ / |''的内容,httplib错误地识别

它作为分隔符。



如果你想阻止URI解析器将某些东西视为URI的一部分,你需要引用它:


pwd = urllib.quote(pwd,"")



试过这个但没有成功。它最终混淆了urllib和

urllib2对象。

我不想导入urllib,urllib2本身想要工作

这件作品代码。


< code>

proxy_user = r''senthil_or''

proxy_password =''| \ / | pr0c''

#proxy_password = r''| \ / | pr0c''


#用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(开场白)

< / code>


谢谢,

Senthil


" Phoe6"写道:


>如果你想阻止URI解析器将某些东西视为URI的一部分,你需要引用它:

pwd = urllib.quote(pwd,"")



尝试了这个但没有成功。它最终混淆了urllib和

urllib2对象。



哦,拜托。 urllib.quote是*函数*;没有办法从为urllib2编写的代码调用该函数

会影响任何东西。


但你可以从urllib2名称空间访问urllib.quote太。它实际上是

非常相同的功能:


>> urllib.quote



<函数引用位于0x00F430F0>


>> urllib2.quote



<函数报价位于0x00F430F0>


(urllib2从urllib导入大量内容)


我不想导入urllib,urllib2本身想要工作

围绕这段代码。



不想解决问题是一个非常糟糕的问题解决策略,真的。


< / F>


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

解决方案

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>


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


"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>


这篇关于httplib.InvalidURL:非数字端口:用于URL中代理密码中的字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆