我已经编写了python代码,该代码可以断开并重新连接到AP,并且可以正常工作。我试图将其转换为Windows服务,但API的connect部分似乎无法正常工作。我不确定为什么。
这是我的剧本
desired_ssid = 'SSID HERE'
desired_key = 'XXXXXXX'
def ConnectionStatus(number):
if number == 0:
return 'DISCONNECTED'
elif number == 1:
return 'SCANNING'
elif number == 2:
return 'INACTIVE'
elif number == 3:
return 'CONNECTING'
elif number == 4:
return 'CONNECTED'
class AppServerSvc(win32serviceutil.ServiceFramework):
_svc_name_ = "WifiMonitor"
_svc_display_name_ = "Wifi Monitor"
_svc_description_ = "This service monitors the wifi interface for a working network connection, restarting it if need be"
def __init__(self, args):
win32serviceutil.ServiceFramework.__init__(self, args)
self.hWaitStop = win32event.CreateEvent(None, 0, 0, None)
def SvcStop(self):
self.ReportServiceStatus(win32service.SERVICE_STOP_PENDING)
win32event.SetEvent(self.hWaitStop)
def SvcDoRun(self):
self.ReportServiceStatus(win32service.SERVICE_RUNNING)
servicemanager.LogMsg(
servicemanager.EVENTLOG_INFORMATION_TYPE,
servicemanager.PYS_SERVICE_STARTED,
(self._svc_name_, ""),
)
try:
self.Main()
except Exception as e:
servicemanager.LogErrorMsg("Error: " + str(e))
def Main(self):
rc = None
wifi = pywifi.PyWiFi()
ifaces = wifi.interfaces()
#Print all the interfaces
servicemanager.LogInfoMsg("\n".join([i.name() for i in ifaces]))
iface = ifaces[0]
while rc != win32event.WAIT_OBJECT_0:
attempts = 0
while not self.ConnectedToInternet() and attempts < 4:
self.ToggleWifi(iface)
attempts = attempts + 1
#Wait thirty seconds before checking again
rc = win32event.WaitForSingleObject(self.hWaitStop, 30000)
def ToggleWifi(self, iface):
try:
servicemanager.LogInfoMsg(f"{ConnectionStatus(iface.status())}")
iface.disconnect()
time.sleep(2)
iface.scan()
#According to api have to wait a bit before calling scan_results()
time.sleep(8)
selected_profile = None
scan_results = iface.scan_results()
servicemanager.LogInfoMsg("\n".join([prof.ssid for prof in scan_results]))
#loop through scan results to find desired AP
for prof in scan_results:
if prof.ssid == desired_ssid:
selected_profile = prof
selected_profile.key = desired_key
break
#connect works when debugging script but doesn't seem to do anything when run as service
iface.connect(selected_profile)
time.sleep(2)
servicemanager.LogInfoMsg(f"{ConnectionStatus(iface.status())}")
except Exception as e:
servicemanager.LogErrorMsg("Error: " + str(e))
def ConnectedToInternet(self, url="http://www.google.com/", timeout=5):
try:
_ = requests.head(url, timeout=timeout)
return True
except requests.ConnectionError:
pass
return False
if __name__ == "__main__":
if len(sys.argv) == 1:
servicemanager.Initialize()
servicemanager.PrepareToHostSingle(AppServerSvc)
servicemanager.StartServiceCtrlDispatcher()
else:
win32serviceutil.HandleCommandLine(AppServerSvc)
我可以看到,当作为服务运行时,所有接口以及所有访问点都记录到事件查看器中。因此,这并不是说该服务根本无法正常工作。当我调用connect()时,它只是没有连接到AP。
将它作为服务运行还是作为脚本运行时,是否与权限差异有关? (我使用pyinstaller将脚本转换为exe)。
Windows 10,x64
编辑:
我正在将此库用于wifi api https://github.com/awkman/pywifi