文章目录

  • 设备信息
  • 一. 下载安装 firefox 浏览器相关驱动
    • 1. firefox 浏览器版本查看
    • 2. 下载对应版本的 selenium 和 geckodriver 驱动
  • 二. 自动连接脚本
  • 三、问题记录
    • 1. Message: Failed to decode response from marionette
    • 2. driver.get(url) 超时
    • 3. requests.exceptions.ConnectionError: HTTPConnectionPool(host='www.baidu.com', port=80): Max retries exceeded with url

由于校园网有连接时长限制,每次超过时间都需要手动进行登录连接,很麻烦。所以使用 python 写了一个脚本,可以自动检测网络是否连接,并且如果断网自动连接校园网。

设备信息

Linux 服务器: CentOS 7
firefox 浏览器版本:Mozilla Firefox 68.5.0esr

建议: 在安装之前可以在linux 服务器上新建一个项目文件夹,以便后续存放文件和日志信息 (本人创建的项目文件夹名 auto_connect

一. 下载安装 firefox 浏览器相关驱动

下载之前先要查看服务器上firefox 浏览器的版本, 并且下载对应版本的selenium 和geckodriver

1. firefox 浏览器版本查看

在服务器终端输入命令

firefox -V

2. 下载对应版本的 selenium 和 geckodriver 驱动

firefox 浏览器版本与selenium 和 geckodriver 版本对应表
geckodriver 下载链接

本人选择的selenium 版本是3.141.0 ; geckodriver 版本是0.29.0
最好在conda 环境下下载, 需要先激活自己的conda 环境

# 安装selenium命令, 在conda 环境下安装
pip install selenium=3.141.0# 查看安装的selenium版本
import selenium
print(help(selenium))
# 1. 下载geckodriver(可以先从本地下载,然后上传到服务器)
wget https://github.com/mozilla/geckodriver/releases/download/v0.29.0/geckodriver-v0.29.0-linux64.tar.gz# 2. 解压, 解压后将 geckodriver.exe 放在新建的项目文件夹auto_connect下
tar --xvzf geckodriver-v0.26.0-linux64.tar.gz# 3. 修改权限
chmod +x geckodriver# 4. 转移,将 geckodriver 复制到 /usr/bin  /usr/local/bin 和python.exe 的目录下
# 复制到 /usr/bin
sudo cp geckodriver /usr/bin# 复制到 /usr/local/bin
sudo cp geckodriver /usr/local/bin# 复制到与python.exe 同目录下,由于本人的环境conda 环境,所以复制到conda 环境下与python.exe 同目录下
sudo cp geckodriver /home/user1/anaconda3/envs/自己的环境名/bin/

二. 自动连接脚本

在新建的auto_connect文件夹下新建auto_connect.py 文件
每次重连的打印信息都会被保存进日志文件auto_connect_log.txt
在conda 环境下运行
在编写脚本之前需要准备的信息:

1. 校园网登录的网址
2. 校园网登录的账号和密码
3. 校园网登陆网页中填写账号、密码和点击登录按钮的相应组件

完整代码

from selenium import webdriver
from selenium.webdriver import FirefoxOptions
from bs4 import BeautifulSoup
import time
import requestsusername_str = "*******"   # 你的校园网登陆用户名
password_str = "********"   # 你的校园网登陆密码
can_connect = Truedef login():profile = webdriver.FirefoxProfile()# 禁止下载图片,根据情况使用profile.set_preference("permissions.default.image", 2)  # 禁用浏览器缓存profile.set_preference("network.http.use-cache", False)profile.set_preference("browser.cache.memory.enable", False)profile.set_preference("browser.cache.disk.enable", False)profile.set_preference("browser.sessionhistory.max_total_viewers", 3)profile.set_preference("network.dns.disableIPv6", True)profile.set_preference("Content.notify.interval", 750000)profile.set_preference("content.notify.backoffcount", 3)# 有的网站支持 有的不支持 2 35 profile.set_preference("network.http.pipelining", True)profile.set_preference("network.http.proxy.pipelining", True)profile.set_preference("network.http.pipelining.maxrequests", 32)# print(getCurrentTime(), '2.0 trying to connect')with open("auto_connect_log.txt", "a+") as f:f.write("\n" + getCurrentTime() + " 0. trying to connect Internet")# 2.1 配置optopts = FirefoxOptions()opts.add_argument("--headless")driver = webdriver.Firefox(options=opts, firefox_profile=profile)try:# print(getCurrentTime(), '2.1 driver get sucessfully')with open("auto_connect_log.txt", "a+") as f:f.write("\n" + getCurrentTime() + " 1. driver init successful")driver.get("***********") # 你的校园网登陆地址driver.refresh()# print(getCurrentTime(), '2.2 get url sucessful')with open("auto_connect_log.txt", "a+") as f:f.write("\n" + getCurrentTime() + " 2. the diver init and address insert successful")# 2.2 获取账号、密码、登录按钮组件# print(driver.find_elements_by_name("DDDDD"))# print(driver.find_elements_by_name("upass"))# print(driver.find_elements_by_name("0MKKey"))username_input = driver.find_elements_by_name("DDDDD")[1]   # 校园网登陆用户名的输入控件ID, 浏览器上右键查看网页源代码查询password_input = driver.find_elements_by_name("upass")[1]   # 校园网登陆密码的输入控件ID, 浏览器上右键查看网页源代码查询login_button = driver.find_elements_by_name("0MKKey")[1]    # 校园网登陆连接的点击控件ID, 浏览器上右键查看网页源代码查询# print(getCurrentTime(), '2.3 username and password get suceessful')with open("auto_connect_log.txt", "a+") as f:f.write("\n" + getCurrentTime() + " 3. the account password and loginButton get successful")# 2.3 发送账号、密码、并点击登录username_input.send_keys(username_str)# print(getCurrentTime(), '2.4 successsfully input name')password_input.send_keys(password_str)# print(getCurrentTime(), '2.5 successsfully input pass')login_button.click()# print(getCurrentTime(), '2.6 successsfully click button')# print(getCurrentTime(), '2.7 connect successfully')with open("auto_connect_log.txt", "a+") as f:f.write("\n" + getCurrentTime() + " 4. Connect successfully \n")except Exception as e:print(getCurrentTime(), e)# 登陆错误print(getCurrentTime(), "2.-1 Error Login")finally:driver.quit()#获取当前时间
def getCurrentTime():return time.strftime('[%Y-%m-%d %H:%M:%S]',time.localtime(time.time()))#判断当前是否可以连网
def handler():try:global can_connectcan_connect = False # 默认判断连接为失败状态# 需要设置timeout是因为断网时request会堵住程序, 设置timeout后最多等待10s,如果没有request成功, can_connect为Falsebaidu_request = requests.get("http://www.baidu.com", timeout=10)if(baidu_request.status_code==200):# print(getCurrentTime(), "1.1 status = 200")baidu_request.encoding = 'utf-8'baidu_request_bsObj = BeautifulSoup(baidu_request.text, 'html.parser')baidu_input = baidu_request_bsObj.find(value="百度一下")if baidu_input==None:return Falsecan_connect = True # 只有可以request 到百度的网址,并且页面中含有“百度一下”这几个字符,才判断连接成功return Trueelse:print(getCurrentTime(), '1.2 Offline')return Falseexcept:print('error')return False#主函数
def main():# 连接校园网脚本开始运行print(getCurrentTime(), u"Hi, The Script of auto_connect start to run")# time.sleep(600)while True:# print(getCurrentTime(), "1. checking the internet")handler()       # 1. 测试是否有网global can_connect# print(can_connect)if not can_connect:# 断网了with open("auto_connect_log.txt","a+") as f:f.write("\n" + getCurrentTime() + " The Internet disconnect, and I am trying to connect it....")# print(getCurrentTime(), "2. The Internet disconnect, and I am trying to connect it....")login()     # 2. 尝试连接else:# 正常# print(getCurrentTime(), "All is normal.....")time.sleep(300)main()

三、问题记录

1. Message: Failed to decode response from marionette

自动连接程序运行了一周都是正常的,但是突然出现这个问题
使用top 命令查看CPU占有率
top 命令详解

top -bn 1 -i -c

可以看到有很多firefox 进程, 进一步查看火狐浏览器进程

ps -aux|grep firefox

删除所有firefox 进程, 重新运行自动连接脚本

pkill firefox

2. driver.get(url) 超时

自动连接脚本成功运行一周,但是突然报错,driver.get(url) 超时, 无法自动连接
查看 auto_connect 文件夹占有的内存大小, 发现竟然占用21G

查看文件夹占有大小
du -h --max-depth=1

解决办法:

1. 定期手动删除服务器上的auto_connect文件夹, 然后从本地重新上传2. 配置页面加载优化和浏览器缓存profile = webdriver.FirefoxProfile()# 禁止下载图片,根据情况使用profile.set_preference("permissions.default.image", 2)  # 禁用浏览器缓存profile.set_preference("network.http.use-cache", False)profile.set_preference("browser.cache.memory.enable", False)profile.set_preference("browser.cache.disk.enable", False)profile.set_preference("browser.sessionhistory.max_total_viewers", 3)profile.set_preference("network.dns.disableIPv6", True)profile.set_preference("Content.notify.interval", 750000)profile.set_preference("content.notify.backoffcount", 3)# 有的网站支持 有的不支持 2 35 profile.set_preference("network.http.pipelining", True)profile.set_preference("network.http.proxy.pipelining", True)profile.set_preference("network.http.pipelining.maxrequests", 32)driver = webdriver.Firefox(options=opts, firefox_profile=profile)3. 每次driver.get(url)刷新窗口driver.get("***************") # 你的校园网登陆地址driver.refresh()

3. requests.exceptions.ConnectionError: HTTPConnectionPool(host=‘www.baidu.com’, port=80): Max retries exceeded with url

HTTPConnectionPool(host=‘www.baidu.com’, port=80): Max retries exceeded with url: / (Caused by NewConnectionError(‘<urllib3.connection.HTTPConnection object at 0x7f576363cf10>: Failed to establish a new connection: [Errno -2] Name or service not known’))

handler() 函数中通过request.get(url) 与 baidu 建立 TCP 连接,默认为keep-alive,即连接一次,传输多次,然而在多次访问后不能结束并回到连接池中,导致不能产生新的连接

Linux 服务器自动连接校园网,selenium + geckodriver + firefox浏览器相关推荐

  1. 【Ubuntu】开启ssh服务/配置ftp内网穿透/自动连接校园网

    前言 想让工作电脑开启ssh服务,这样就可以在校外进行远程访问办公,电脑的系统为Ubuntu20.04 开启ssh服务 首先查看当前Ubuntu安装的SSH服务: dpkg -l | grep ssh ...

  2. 远程连接Linux服务器无法连接解决办法

    1.查看SSH是否安装(检查是否装了SSH包) 输入命令:rpm -qa | grep ssh 如下如所示系统已经默认安装了SSH: 远程连接Linux服务器无法连接解决办法 若没有安装,则输入 yu ...

  3. Python脚本-自动连接校园网

    自动连接校园网 使用语言:Python python爬虫 浏览器:谷歌浏览器 import requests#登录地址 URL="http://10.2.255.26:801/eportal ...

  4. 南信大电脑开机自动连接校园网

    2022-11-20更新:南信大更新了连接时的url,更改部分如下: val url ="http://10.255.255.46/api/v1/login"val ipPath ...

  5. pyhon使用pip安装卸载selenium和安装firefox驱动,及使用selenium启动firefox浏览器

    使用selenium的时候要注意版本兼容问题,使用selenium2.x的时候,firefox必须47以下,可以不需要安装webdriver驱动.但是使用selenium3.x的时候,firefox版 ...

  6. 解决Selenium与firefox浏览器版本不兼容问题

    解决Selenium与firefox浏览器版本不兼容问题 参考文章: (1)解决Selenium与firefox浏览器版本不兼容问题 (2)https://www.cnblogs.com/limxia ...

  7. linux远程windows执行cmd,Linux服务器远程连接window服务器并执行cmd命令

    前段时间,要给一个分布式调度系统写一个运维脚本,这个分布式调度系统部分子系统部署在window服务器上,这个时候就要想办法用Linux远程来连接window服务器,并执行cmd命令.下面是我的解决方法 ...

  8. 地下城与勇士正在自动连接频道服务器,自动连接频道失败 无限自动连接如何处理...

    自古以来,DNF就有着"掉线城"和"BUG城"的美称.由于服务器问题,勇士们在玩DNF时经常会碰到网络连接中断的情况,非常闹心.还有就是每一次游戏更新都会出现一 ...

  9. qt之TCP/IP通信客户端与服务器自动连接互相传输数据

    一.前言 在开发上位机软件,用的最多的通信就是网络通信,串口通信和USB通信,串口通信相对是最容易的,USB通信在前面的文章中也专门写了一个博客介绍USB HID通信方式,见链接:https://bl ...

最新文章

  1. 【CVPR2020】30篇最新论文抢先看!!!
  2. Python将py文件生成exe文件
  3. vue-cli3插件初体验
  4. Maven(五)使用Nexus搭建Maven私服
  5. 变相裁员??全员营销!程序员被要求卖房~
  6. SQL Server还原和一些小发现
  7. SANXIN-B01开发板verilog教程V3电子版
  8. 软件测试过程的四个阶段(单元测试、集成测试、系统测试、验收测试)
  9. 数据分析与挖掘建模(理论知识)
  10. centos安装python3.8.1_在CentOS 8上编译安装Python 3.8.1
  11. Python 寻峰算法
  12. 粉末成型工艺(粉末冶金粉末注射成型)
  13. 一个强大图片的选择、裁剪工具—看这一个就够用了
  14. 60个必备NOIP模板 python算法模板
  15. 数据库可移植性重要吗?
  16. no main manifest attribute maven package 运行打包后的jar包报错:xxx.jar中没有主清单属性 通过配置 maven plugin 解决
  17. Oracle多表查询 –工作中避免笛卡儿积的出现
  18. 一键制作生日快乐的网站_生日快乐和一本新书
  19. 利用第三方解码器ffmpeg让群晖DSM6.2.4版本的Video Station支持DTS视频编码和EAC3音频编码
  20. 跑路不止、“幽灵所”激增,加密货币交易行业加速洗牌 |链捕手

热门文章

  1. C语言中scanf()函数的返回值
  2. vue之原生上传图片并压缩图片大小(1)
  3. ps将背景变成透明背景
  4. 中国IM企业的新机会?揭秘融云全球通信云网络背后的技术 | 对话 WICC
  5. 【xlwings api语言参考】Range.VerticalAlignment 属性
  6. Oracle卸载卸不干净,Oracle彻底删除的办法(winxp)
  7. 笔记本安装固态涉及到的注意事项
  8. 机器学习数学基础知识
  9. soft-nms(softnms)(pytorch实现) softer nms
  10. 工地人员定位原理又是什么--新导智能