被上面要求要開網頁,要能判斷404

所以採用google driver,但新版的google driver要在註冊表寫東西

所以寫完整一點

這程式有包含:
a.網卡忽然被disable或被拔起來,錯誤

b.判斷你一開始wifi連線的SSID和密碼正確性 (這邊限定AP使用WPA2PSK 加密)

c.ping 到時,會自動開網頁和ping 到,但網頁開不了的判斷

d.網頁的回傳值 200 ok,或404

e.ping 不到時後的錯誤

f.AP crush的錯誤

g.還要把錯誤都記錄起來 ,

h.會自動開網頁喔!!!

程式如下

import pywifi
import time
import os
import win32api, win32con
from pywifi import const
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from urllib.request import Request, urlopen
from urllib.error import URLError, HTTPError


try:
    wifi = pywifi.PyWiFi()  # 抓取WiFi接口
    ifaceList = wifi.interfaces()  # 抓取无线网卡列表
    ifaces = ifaceList[0]  # 如果有无线网卡第一个一般就是你要的

    ifaces.disconnect()  # 断开网卡连接
    time.sleep(3)  # 缓冲3秒
    profile = pywifi.Profile()  # 配置文件
    t=1

    while(t==1):
        ssid1 = input("Enter SSID:")
        profile.ssid = str(ssid1)  # wifi名称
        profile.auth = const.AUTH_ALG_OPEN  # 需要密码
        profile.akm.append(const.AKM_TYPE_WPA2PSK)  # 加密类型
        profile.cipher = const.CIPHER_TYPE_CCMP  # 加密单元
        key1 = input("Enter SSID'S PASSWORD:")
        profile.key = str(key1)

        ifaces.remove_all_network_profiles()  # 删除其他配置文件
        tmp_profile = ifaces.add_network_profile(profile)  # 加载配置文件
        print("\ntest wifi SSID and PASSWORD")
        ifaces.connect(tmp_profile)  # 连接
        time.sleep(6)  # 尝试5秒能否成功连接
        if (ifaces.status() == const.IFACE_CONNECTED):
            ping = os.system("ping  " + "8.8.8.8")
            print("\nSSID and PASSWORD are   correct")
            t = 0
        else:
            print("\nSSID or PASSWORD is not   correct")
            t = 1
        print("\n")
        ifaces.disconnect()  # 断开连接
        time.sleep(5)  # 尝试5秒能否成功连接
    reg_root = win32con.HKEY_LOCAL_MACHINE
    reg_path = r"SOFTWARE\Policies\Google\Chrome"
    reg_flags = win32con.WRITE_OWNER | win32con.KEY_WOW64_64KEY | win32con.KEY_ALL_ACCESS

    # 直接创建(若存在,则为获取)
    key, _ = win32api.RegCreateKeyEx(reg_root, reg_path, reg_flags)

    # 设置项
    win32api.RegSetValueEx(key, "MachineLevelUserCloudPolicyEnrollmentToken", 0, win32con.REG_SZ, '')

    # 关闭
    win32api.RegCloseKey(key)
    isok = True
    I=1
    FA=0
    while (isok == True):
        fo = open('Error_log.txt', 'a')

        print("WIFI ON")
        ifaces.connect(tmp_profile)  # 连接
        time.sleep(5)  # 尝试5秒能否成功连接

        if(ifaces.status() == const.IFACE_CONNECTED):
            ping = os.system("ping " + "www.google.com")
            if (ping == 0):
                 print("connection Success\n")
                 chrome_options = Options()
                 driver = webdriver.Chrome("./chromedriver")

                 driver.get("http://www.google.com")
                 driver.set_window_position(0, 0)  # 瀏覽器位置
                 driver.set_window_size(700, 700)  # 瀏覽器大小
                 time.sleep(5)
                 driver.close()
                 time.sleep(1)
                 #'http://www.python.org/fish.html' test 404
                 req = Request('http://www.google.com')
                 try:

                     response = urlopen(req)
                     print("reponse:"+str(response.status))

                 except HTTPError as e:
                     print('The server couldn\'t fulfill the request.')
                     fo.write('The server couldn\'t fulfill the request.'+ "\n")
                     print('Error code: ', e.code)
                     fo.write("Error code: " + str(e.code) + "\n")
                     localtime = time.asctime(time.localtime(time.time()))
                     fo.write("time : " + str(localtime) + "\n\n")
                     FA = FA + 1
                 except URLError as e:
                     print('We failed to reach a server.')
                     print('Reason: ', e.reason)
                     fo.write("We failed to reach a server." + "\n")
                     fo.write("Reason:  " + str(e.reason) + "\n")
                     localtime = time.asctime(time.localtime(time.time()))
                     fo.write("time : " + str(localtime) + "\n\n")
                     FA = FA + 1
            else:

                print("wifi on ,but ping fail\n")
                fo.write("wifi on ,but ping fail " + "\n")
                localtime = time.asctime(time.localtime(time.time()))
                fo.write("time : " + str(localtime) + "\n\n")
                FA=FA+1
                time.sleep(2)
        else :
            time.sleep(2)
            #ifaces.disconnect()  # 断开连接
            print("\nUnable to connect") # 無法連結
            fo.write("Unable to connect" + "\n")
            localtime = time.asctime(time.localtime(time.time()))
            fo.write("time : " + str(localtime) + "\n\n")
            FA = FA + 1


        fo.close()
        ifaces.disconnect()  # 断开连接
        time.sleep(4)
        print("\nWIFI OFF")
        #os.system("ping " + str("8.8.8.8"))
        print("\n")
        time.sleep(2)
        print("times="+str(I))
        print("Fail= " +str(FA)+"\n")
        I = I + 1
        if (FA == 80):
            isok = False

    print("\nStopped  test WIFI")
    fo = open('Error_log.txt', 'a')
    fo.write("\n"+"Stopped  test WIFI"+"\n")
    fo.write("RUN "+str(I)+" times\n")
    fo.write( "FAIL:" +str(FA)+"\n")
    fo.close()

except ValueError:
    print("Wireless card is OFF")

 

 

arrow
arrow
    全站熱搜
    創作者介紹
    創作者 qqo9110 的頭像
    qqo9110

    阿藏哥的部落格~不應該阿

    qqo9110 發表在 痞客邦 留言(0) 人氣()