【发布时间】:2012-02-07 22:36:38
【问题描述】:
我今天尝试编写一个小 Python 脚本,但失败了。为什么下面的代码被shell调用后出现如下错误?
错误
File "./testmod.py", line 15, in <module>
printdnsfile(sys.argv[1])
File "./testmod.py", line 10, in printdnsfile
print(socket.gethostbyname(str(line)))
socket.gaierror: [Errno 8] nodename nor servname provided, or not known
代码
#!/usr/bin/python
def printdnsfile(file):
file= open (file,"r")
import socket
dest = open("/dnsfile.txt",'w')
for line in file:
print(socket.gethostbyname(str(line)))
print>>dest, str(",".join([line,socket.gethostbyname(line)])+'\n')
if __name__ == "__main__":
import sys
printdnsfile(sys.argv[1])
我在 python-console 中测试了 socket 模块,它按预期工作。是我的代码有错误还是我的配置有问题?
谢谢。
【问题讨论】: