【发布时间】:2017-09-21 13:18:43
【问题描述】:
我希望用户通过输入要查找的 ip 地址来停留在 while 循环中,当提示他们是否要通过键入 y 或 n 继续时,他们可以继续查找 ip,但经常发生的异常是因为不是所有的 IP 地址都找到了,我遇到了一个无限循环,所以我现在只是让它跳出循环。异常后如何继续询问用户是否想查找另一个 IP 而不是跳出循环?谢谢。
import socket
print("\n----------Look up Domain by IP Address----------\n")
response3 = input("Enter an IP Address: ")
while True:
try:
domain = socket.gethostbyaddr(response3)[0]#.split(".")[1]
print("\nDomain Name is", domain)
except(socket.error):
print("\nA domain name could not be found.")
break
response4 = input("Would you like to look up another IP adress? type y for [yes] or n for [no]: ")
if response4 == "y":
response3 = input("Enter an IP Address: ")
elif response4 == "n":
print("\n[END]")
break
【问题讨论】:
标签: input while-loop infinite-loop serversocket