2016-07-04 20 views
0

我嘗試通過GUI使用插座模塊來解決主機我做了使用Tkinter的 這裏是代碼的一部分,主要的問題是我收到的錯誤在解析路由器的名字的Python 3 - Tkinter的,忽略socket.herror並繼續

for line in p.stdout: 
      fiw = open("1.txt", '+a') 
      line = str(line) 
      if "Received = 1" in line: 
       hostad = socket.gethostbyaddr(ip3 +str(i)) 
       if hostad: 
        try: 
         print(hostad) 
        except socket.herror: 
         print(hostad) 
       fiw.write("Received reply from " + ip3 +str(i)+"\n") 
       print("Received reply from " + ip3 +str(i)+"\n") 
       print(socket.gethostbyaddr(ip3 +str(i))) 

錯誤:

socket.herror: [Errno 11004] host not found 

腳本不會進一步運行,我用print這裏只是舉例還我試圖pass 試圖

except socket.herror as err: 
       print(err) 
       pass 

也嘗試過使用pass在這種方法

回答

0

你必須嘗試...除了周圍的錯誤ŧ的

hostad = socket.gethostbyaddr(ip3 +str(i)) 
if hostad: 
    try: 
     print(hostad) 
    except socket.herror: 
     print(hostad) 

興 而是應該像

try: 
    hostad = socket.gethostbyaddr(ip3 +str(i)) 
    print(hostad) 
except socket.herror: 
    pass # code to execute in case of error 

錯誤消息本身指的是「反向DNS查找失敗」 如Python Sockets: gethostbyaddr : Reverse DNS Lookup Failure

解釋如果線形成的主機名

hostad = socket.gethostbyaddr(ip3 +str(i)) 

沒有反向DNS記錄,拋出異常。

+0

我得到相同的錯誤 – None

+0

你知道其他的解決方案來獲取設備版本/名稱,如nmap嗎? – None

-2

我認爲你應該看看這個thread

例外就好像一個破發點,它打破了你的for循環..

嘗試這樣的:

for line in p.stdout: 
    try: 
     fiw = open("1.txt", '+a') 
     line = str(line) 
     if "Received = 1" in line: 
      hostad = socket.gethostbyaddr(ip3 +str(i)) 
      if hostad: 
       try: 
        print(hostad) 
       except socket.herror: 
        print(hostad) 
      fiw.write("Received reply from " + ip3 +str(i)+"\n") 
      print("Received reply from " + ip3 +str(i)+"\n") 
      print(socket.gethostbyaddr(ip3 +str(i))) 
    except: 
      pass 
+0

'內部嘗試...除了'是沒有意義的,'print'從不拋出'socket.herror'。外面的'try ... except'捕捉每一個通常是不好的習慣的例外。 –

+0

感謝您的輸入!我不知道:) –