我曾试图在网上找到解决方案,但没有为我找到完整的解决方案。
我尝试通过合并代码和文件中的变量来创建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)
它正常工作。请帮助并感谢你。
答案 0 :(得分:0)
我很抱歉这篇文章。更改了控制台宽度,发现我的字符串是从末尾带有\n
的文件中读取的,因此URL已损坏。
所以解决方案是:
str(RRPTR).replace("-", "%2D").replace('\n', "")
作为我网址的一部分。