Efficiency with "try...except"
The code works fine. I just had an efficiency question. In this code I'm using "try
...except
" to pass an error that would crash the program. The error is caused by the IP address not being reachable. As I said before, the code do开发者_StackOverflowes its job. I want to know if this the quickest way to see if the IP is reachable and pass it up. It stalls for 30 to 45 seconds if the IP can't be found. Piled up a handful of times this could be rather time consuming.
import win32net
def GetUsers( IP ):
try:
Users = win32net.NetGroupGetUsers(IP,'none',0),
except:
print IP + ': Was not found!'
else:
print IP + ': ' + Users
return
F = open("C:\Users\*User*\Desktop\IP_List.txt")
for CurrentIP in F.readlines():
GetUsers(CurrentIP.strip()),
F.close()
print 'DONE!'
Your question about efficiency is absolutely unrelated to your use of try
... except
. Exception handling in Python is relatively fast, about the same as running any other Python statement (we're talking on the order of microseconds).
Your 30-45 second performance is undoubtedly due to the behaviour of NetGroupGetUsers
when the IP address is unreachable.
精彩评论