校园网断网自动连接的方法(适用于远程控制电脑)
` 提示:该文章是在参考以下的博文编写而成。(因为参考的文章里有两个地方需要修改,所以这篇文章着重介绍一下修改的地方)
参考文章:
校园网自动重连,还你一个一直在线的服务器
在windows中使用SIGALRM
WebDriver鼠标、键盘操作
文章目录
前言
提示:这里可以添加本文要记录的大概内容:
提示:以下是本篇文章正文内容,下面案例可供参考
一、环境的配置
1.python3.7及以上的运行环境
2.pip安装selenium包
3.下载webdriver firefox内核(python可以操控的浏览器,跟我们平时用的浏览器不同,代码中使用Chrome的driver),下载地址:
https://github.com/mozilla/geckodriver/releases 超链接:chrome_driver
然后解压缩下载的文件到一个文件夹,如果是linux则复制到/usr/bin/geckodriver, 如果是windows则复制它解压后的绝对路径或相对路径,这样python运行时可以找到这个driver。linux 复制该driver的代码:
sudo cp geckodriver /usr/bin/geckodriver
二、具体的使用
1.修改代码的校园网用户名,校园网密码,校园网登陆地址
校园网用户名输入控件ID,校园网密码输入控件ID,以及校园网连接控件ID。以上几个ID都可以通过查看网页源码得到。
代码如下(示例):
def login():
try:
driver = webdriver.Chrome('C:\\Users\\***\\chromedriver_win32\\chromedriver.exe')
driver.get("http://222.198.127.170/") # 你的校园网登陆地址
time.sleep(3)
input1 = driver.find_element(By.ID, "username") # 校园网登陆用户名的输入控件ID, 浏览器上右键查看网页源代码查询
input1.send_keys("***")
input1.send_keys(Keys.TAB)
driver.find_element(By.ID, "pwd").send_keys("***") # 校园网登陆密码的输入控件ID, 浏览器上右键查看网页源代码查询
input1.send_keys(Keys.ENTER)
#driver.find_element(By.ID, "selectDisname").send_keys("默认")
#print('Searching connect')
#driver.find_element(By.CLASS_NAME, "loginButtonHKClicked_1").click() # 校园网登陆连接的点击控件ID, 浏览器上右键查看网页源代码查询
print('Find connect successfully')
#username_input.send_keys(username_str)
#password_input.send_keys(password_str)
print('Input user info')
print('Connect')
except:
print(getCurrentTime(), u"登陆函数异常")
finally:
driver.close()
三、修正的内容
1.将Linux的signal.SIGALRM替换成windows的线程处理
原文章代码:
def main():
print (getCurrentTime(), u"Hi,校园网自动登陆脚本正在运行")
while True:
while True:
start_time = time.time()
signal.signal(signal.SIGALRM, handler)
signal.alarm(10)
end_time = time.time()
print(end_time - start_time)
global can_connect
print(can_connect)
if not can_connect:
print (getCurrentTime(),u"断网了...")
try:
login()
except:
print(getCurrentTime(), u"浏览器出了bug")
else:
print (getCurrentTime(), u"一切正常...")
time.sleep(5)
time.sleep(10)
time.sleep(10)
修改后代码:
def main():
print (getCurrentTime(), u"Hi,校园网自动登陆脚本正在运行")
while True:
while True:
start_time = time.time()
t = Ticker(1.0) # make a ticker
t.start() # start the ticker in a new thread
try:
while t.evt.wait(): # hang out til the time has elapsed
handler()
global can_connect
print(can_connect)
if not can_connect:
print(getCurrentTime(), u"断网了...")
try:
login()
except:
print(getCurrentTime(), u"浏览器出了bug")
else:
print(getCurrentTime(), u"一切正常...")
time.sleep(5)
t.evt.clear() # tell the ticker to loop again
print(time.time(), "FIRING!")
except:
t.stop() # tell the thread to stop
t.join() # wait til the thread actually dies
end_time = time.time()
print(end_time - start_time)
time.sleep(10)
time.sleep(10)
后来发现一个更好的解决方案,链接放在下面:
https://github.com/daili0015/SEU_Net_Connect/blob/master/main_webdriver.py
2.将获取网页ID的代码块修改了一下,原因是发现光标一直停留在第一个用户名输入框里,而不会自动切换到密码输入框。
原文章代码:
def login():
try:
driver = webdriver.Firefox()
driver.get("http://net.********.edu.cn") # 你的校园网登陆地址
time.sleep(3)
username_input = driver.find_element_by_id("uname") # 校园网登陆用户名的输入控件ID, 浏览器上右键查看网页源代码查询
password_input = driver.find_element_by_id("pass") # 校园网登陆密码的输入控件ID, 浏览器上右键查看网页源代码查询
print('Searching connect')
login_button = driver.find_element_by_id("connect") # 校园网登陆连接的点击控件ID, 浏览器上右键查看网页源代码查询
print('Find connect successfully')
username_input.send_keys(username_str)
password_input.send_keys(password_str)
print('Input user info')
login_button.click()
print('Connect')
except:
print(getCurrentTime(), u"登陆函数异常")
finally:
driver.close()
修改后的代码:
def login():
try:
driver = webdriver.Chrome('C:\\Users\\****\\chromedriver_win32\\chromedriver.exe')
driver.get("http://222.198.127.170/") # 你的校园网登陆地址
time.sleep(3)
input1 = driver.find_element(By.ID, "username") # 校园网登陆用户名的输入控件ID, 浏览器上右键查看网页源代码查询
input1.send_keys("****")
input1.send_keys(Keys.TAB)
driver.find_element(By.ID, "pwd").send_keys("****") # 校园网登陆密码的输入控件ID, 浏览器上右键查看网页源代码查询
input1.send_keys(Keys.ENTER)
#driver.find_element(By.ID, "selectDisname").send_keys("默认")
#print('Searching connect')
#driver.find_element(By.CLASS_NAME, "loginButtonHKClicked_1").click() # 校园网登陆连接的点击控件ID, 浏览器上右键查看网页源代码查询
print('Input user info')
print('Connect')
except:
print(getCurrentTime(), u"登陆函数异常")
finally:
driver.close()