Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
# Copyright 2017 Jing Xu
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
from typing import *
from requests import Session
from requests.auth import HTTPBasicAuth
from bs4 import BeautifulSoup
import re
from tkinter import *
import tkinter.messagebox as messagebox
import sys
class WiFiController:
def __init__(self):
self.username = 'admin'
self.passowrd = 'Mrblood!'
self.control_url = 'http://192.168.123.1'
self.session = Session()
self.auth = HTTPBasicAuth(username=self.username, password=self.passowrd)
def enable_guest_wifi(self) -> bool:
post_payload = r'current_page=%2FAdvanced_WGuest2g_Content.asp&next_page=&next_host=192.168.123.1&sid_list=WLANConfig11b%3B&group_id=&action_mode=+Apply+&action_script=&rt_gmode=2&rt_country_code=US&rt_guest_ssid_org=home_guest&rt_guest_wpa_mode=2&rt_guest_wpa_psk_org=07735606510&rt_guest_date_x=1111111&rt_guest_time_x=00002359&rt_guest_time2_x=00002359&rt_guest_enable=1&rt_guest_date_x_Mon=on&rt_guest_date_x_Tue=on&rt_guest_date_x_Wed=on&rt_guest_date_x_Thu=on&rt_guest_date_x_Fri=on&rt_guest_time_x_starthour=00&rt_guest_time_x_startmin=00&rt_guest_time_x_endhour=23&rt_guest_time_x_endmin=59&rt_guest_date_x_Sat=on&rt_guest_date_x_Sun=on&rt_guest_time2_x_starthour=00&rt_guest_time2_x_startmin=00&rt_guest_time2_x_endhour=23&rt_guest_time2_x_endmin=59&rt_guest_ssid=home_guest&rt_guest_closed=0&rt_guest_lan_isolate=1&rt_guest_ap_isolate=1&rt_guest_mcs_mode=7&rt_guest_auth_mode=psk&rt_guest_crypto=aes&rt_guest_wpa_psk=07735606510&rt_guest_macrule=0'
resp = self.session.post(self.control_url + '/start_apply.htm', data=post_payload, auth=self.auth)
def disable_guest_wifi(self) -> bool:
post_payload = r'current_page=%2FAdvanced_WGuest2g_Content.asp&next_page=&next_host=192.168.123.1&sid_list=WLANConfig11b%3B&group_id=&action_mode=+Apply+&action_script=&rt_gmode=2&rt_country_code=US&rt_guest_ssid_org=home_guest&rt_guest_wpa_mode=2&rt_guest_wpa_psk_org=07735606510&rt_guest_date_x=1111111&rt_guest_time_x=00002359&rt_guest_time2_x=00002359&rt_guest_enable=0&rt_guest_date_x_Mon=on&rt_guest_date_x_Tue=on&rt_guest_date_x_Wed=on&rt_guest_date_x_Thu=on&rt_guest_date_x_Fri=on&rt_guest_time_x_starthour=00&rt_guest_time_x_startmin=00&rt_guest_time_x_endhour=23&rt_guest_time_x_endmin=59&rt_guest_date_x_Sat=on&rt_guest_date_x_Sun=on&rt_guest_time2_x_starthour=00&rt_guest_time2_x_startmin=00&rt_guest_time2_x_endhour=23&rt_guest_time2_x_endmin=59&rt_guest_ssid=home_guest&rt_guest_closed=0&rt_guest_lan_isolate=1&rt_guest_ap_isolate=1&rt_guest_mcs_mode=7&rt_guest_auth_mode=psk&rt_guest_crypto=aes&rt_guest_wpa_psk=07735606510&rt_guest_macrule=0'
resp = self.session.post(self.control_url + '/start_apply.htm', data=post_payload, auth=self.auth)
def check_wifi_status(self) -> bool:
resp = self.session.get(self.control_url + '/system_status_data.asp', auth=self.auth)
json_data_str = resp.content.decode()
res = re.compile(r'(guest:) (\d)').findall(json_data_str)
if res[0][1] == '0':
return False
# else:
return True
def check_available(self) -> bool:
availability = False
resp = self.session.get(self.control_url, auth=self.auth)
if resp.status_code == 200:
soup = BeautifulSoup(resp.content, 'html.parser')
if len(soup.select('span#logined_ip_str')) != 0:
pass
else:
availability = True
self.session.get(self.control_url + '/Logout.asp')
return availability
def logout(self):
self.session.get(self.control_url + '/Logout.asp')
class Application(Frame):
def __init__(self, master=None):
# super().__init__(self, master)
Frame.__init__(self, master)
self.pack()
self.controller = WiFiController()
if self.controller.check_available() is False:
messagebox.showinfo('错误', '当前已有登录用户,请等待5分钟再试。')
sys.exit(0)
Label(self, text="当前状态:").pack()
self.status_text = StringVar(self)
self.status_text.set("已开启" if self.controller.check_wifi_status() else "已关闭")
Label(self, textvariable=self.status_text).pack()
self.control_button_text = StringVar(self)
self.control_button_text.set("开启访客WiFi" if self.controller.check_wifi_status() is False else "关闭访客WiFi")
self.control_button = Button(self, textvariable=self.control_button_text, command=self.change_wifi_status)
self.control_button.pack()
def change_wifi_status(self):
if self.controller.check_wifi_status():
self.controller.disable_guest_wifi()
else:
self.controller.enable_guest_wifi()
self.controller.logout()
sys.exit(0)
def main():
app = Application()
app.mainloop()
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.