【问题标题】:Urlopen error when URL created by reading from file通过从文件读取创建 URL 时出现 Urlopen 错误
【发布时间】:2018-04-03 11:48:33
【问题描述】:

我曾尝试在网络上找到解决方案,但没有找到适合我的完整解决方案。

我正在尝试通过连接代码和文件中的变量来创建 URL,但 urlopen() 返回 urllib2.URLError: <urlopen error no host given>

这是我的代码:

#!/usr/bin/env python
# _*_ coding: utf-8 _*_

from urllib2 import urlopen
from billmgrvars import * 
# These variables are from billmgrvars file
url = "https://" + dnsmgrIP + ":" + dnsmgrPort + "/dnsmgr?authinfo=" + resellerlogin + ":" + resellerpass + "&out=json&sform=ajax"

with open ("PTRlist.py") as PTRfile:
    for RRPTR in PTRfile.readlines():
        print (RRPTR)
        addPTRurl = url + "&clicked_button=ok&dtype=master&email=tech%40example.com&func=domain.edit&ip=&masterip=&name=" + str(RRPTR).replace("-", "%2D") + "&progressid=false&sok=ok&zoom-ip="
        print "URL: " + addPTRurl
        urlopen(addPTRurl)
PTRfile.close()

PTRlist.py 文件:

102.213.62.in-addr.arpa
43.24.212.in-addr.arpa

运行脚本:

102.213.62.in-addr.arpa

URL: https://1.2.3.4:1500/dnsmgr?authinfo=user:password&out=json&sform=ajax&clicked_button=ok&dtype=master&email=tech%40example.com&func=domain.edit&ip=&masterip=&name=102.213.62.in%2Daddr.arpa
&progressid=false&sok=ok&zoom-ip=
Traceback (most recent call last):
  File "./getPTRs.py", line 28, in <module>
    urlopen(addPTRurl)
  File "/usr/lib64/python2.6/urllib2.py", line 126, in urlopen
    return _opener.open(url, data, timeout)
  File "/usr/lib64/python2.6/urllib2.py", line 389, in open
    req = meth(req)
  File "/usr/lib64/python2.6/urllib2.py", line 1096, in do_request_
    raise URLError('no host given')
urllib2.URLError: <urlopen error no host given>

不知道为什么会出现这个错误。

如果我只是运行如下脚本:

testVariable = "https://1.2.3.4:1500/dnsmgr?authinfo=user:password&out=json&sform=ajax&clicked_button=ok&dtype=master&email=tech%40example.com&func=domain.edit&ip=&masterip=&name=102.213.62.in%2Daddr.arpa
    &progressid=false&sok=ok&zoom-ip="
urlopen(testVariable)

它工作正常。请帮忙,谢谢。

【问题讨论】:

    标签: python python-2.7 urlopen


    【解决方案1】:

    我很抱歉那个帖子。更改控制台宽度,发现我的字符串是从末尾带有\n 的文件中读取的,因此 URL 已损坏。

    所以解决办法是:

    str(RRPTR).replace("-", "%2D").replace('\n', "") 作为我的 URL 的一部分。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-03-03
      • 2020-08-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多