简单python modu中的错误

2024-09-30 10:37:30 发布

您现在位置:Python中文网/ 问答频道 /正文

我今天试着写一个小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

代码

^{pr2}$

我在python控制台中测试了socket模块,它的工作与预期一样。我的代码有错误还是配置有问题?在

谢谢。在


Tags: 代码inpy脚本错误syslinesocket
2条回答

输入文件中可能有一个空行。尝试在获取hostbyname之前检查一下您的线路。在

def printdnsfile(file):
    file= open (file,"r")
    import socket
    dest = open("/dnsfile.txt",'w')
    for line in file:
        line = line.strip()
        if line:
            print(socket.gethostbyname(str(line)))
            print>>dest, str(",".join([line,socket.gethostbyname(line)])+'\n')

问题可能是line不包含预期值。为了确保您可以在失败的行之前添加print line语句,或者使用^{}来调试程序。在

相关问题 更多 >

    热门问题