python - Win32 exception occurred when using pythoncom.CoUninitialize() and wmi -
python - Win32 exception occurred when using pythoncom.CoUninitialize() and wmi -
i trying create create function in python 3.4 ping server. needs work on windows (possibly xp , above).
i have found can utilize wmi win32_pingstatus (reference) class ping server , have written next function:
import sys if sys.platform.startswith('win32'): import wmi import pythoncom def ping(ip_address): """ check if can ping ip address using os ping tool""" if sys.platform.startswith('win32'): try: pythoncom.coinitialize() c = wmi.wmi() status_code = c.win32_pingstatus(address=ip_address)[0].statuscode if status_code == 0: successful = true else: successful = false pythoncom.couninitialize() except wmi.x_wmi: # if wmi ping fails fallback pass else: raise notimplementederror homecoming successful if __name__ == "__main__": ping("127.0.0.1")
this works intended returns true when can ping ip , false when can't. every time run series of errors @ end:
win32 exception occurred releasing iunknown @ 0x03140980 win32 exception occurred releasing iunknown @ 0x031635d0 win32 exception occurred releasing iunknown @ 0x03163560
this happens every time run script although first 2 bytes of address alter each time. lastly 2 bytes remain same.
i have tried commenting out various sections of code , have found if remove pythoncom.couninitialize()
exceptions not occur. using pythoncom.coinitialize()
, pythoncom.couninitialize()
want phone call function in thread described here
i have tried adding print(pythoncom._getinterfacecount())
see how many interfaces nowadays , have noticed each time function run interfaces increment 6 , not cut down not cut down downwards under 10 ever.
does know why exception occurring , right way deal is?
thank you
i think have managed prepare problem. seems should phone call pythoncom.coinitialize()
in separate thread pythoncom automatically calls on main thread http://docs.activestate.com/activepython/2.5/pywin32/pythoncom__coinitializeex_meth.html.
so check if current thread main thread , if don't phone call pythoncom.coinitialize()
, pythoncom.couninitialize()
.
python winapi wmi
Comments
Post a Comment