Python:Urllib.urlopen 非数字端口

发布于 2024-09-11 09:00:08 字数 648 浏览 4 评论 0原文

对于以下代码,

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

我收到以下错误

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

有什么想法吗???

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

友欢 2024-09-18 09:00:08

您可能需要对密码进行 url 编码。如果密码恰好包含“/”,您将看到类似的错误。

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

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

对密码进行编码:

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

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

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.

梦幻的心爱 2024-09-18 09:00:08

我同意muckabout,这就是问题所在。
您可能习惯在浏览器中使用它,这将导致浏览器向主机进行身份验证。您可能应该删除第一个@符号之前的所有内容。

查看 urllib 文档,特别是 FancyURLOpener,它可能会解决您的身份验证问题。

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.

最单纯的乌龟 2024-09-18 09:00:08

该错误消息表明您正在准备的网址存在一些问题。打印并检查这是否是有效的网址。

The error message shows that there is some issue with the url that you are preparing. Print and check if this is a valid url.

葬花如无物 2024-09-18 09:00:08

假定 HTTP URL 中的“:”位于端口号之前。您输入的帐户名称不是数字。它必须是整数端口值。

The ':' in the HTTP URL is assumed to precede a port number. You are placing an account name which is not numeric. It must be an integer port value.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文