Python:Urllib.urlopen 非数字端口 [英] Python: Urllib.urlopen nonnumeric port

查看:34
本文介绍了Python:Urllib.urlopen 非数字端口的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对于下面的代码

theurl = "https://%s:%s@members.dyndns.org/nic/update?hostname=%s&myip=%s&wildcard=NOCHG&mx=NOCHG&backmx=NOCHG" %(用户名、密码、主机名、ip)conn = urlopen(theurl) # 向url发送请求print(conn.read()) # 读取响应conn.close() # 关闭连接

我收到以下错误

File "c:\Python31\lib\http\client.py", line 667, in _set_hostportraise InvalidURL("非数字端口:'%s'" % host[i+1:])

有什么想法吗???

解决方案

您可能需要对密码进行 url 编码.如果密码恰好包含/",您会看到类似的错误.

这是一个本地示例(实际值已编辑):

<预><代码>>>>开瓶器>>>opener.open('http://admin:somepass@example.com')<addinfourl at 3068618924L 其 fp = <socket._fileobject object at 0xb6e7596c>>>>>opener.open('http://admin:somepass/a@example.com')*** InvalidURL: 非数字端口: 'somepass'

对密码进行编码:

<预><代码>>>>opener.open('http://admin:somepass%2Fa@example.com')

您可以使用 urllib.quote('somepass/a', safe='') 进行编码.

for the following code

theurl = "https://%s:%s@members.dyndns.org/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???

解决方案

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:somepass@example.com')
<addinfourl at 3068618924L whose fp = <socket._fileobject object at 0xb6e7596c>>
>>> opener.open('http://admin:somepass/a@example.com')
*** InvalidURL: nonnumeric port: 'somepass'

Encode the password:

>>> opener.open('http://admin:somepass%2Fa@example.com')

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

这篇关于Python:Urllib.urlopen 非数字端口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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