为了实现 断网了,自动连接网络

原理:每隔一段时间ping一下百度,判断网络状态,没有联网的话,就模仿浏览器发一条Post给服务器

import urllib
import hashlib
import subprocess
import os
import time

def getPass(password):
pass1 = hashlib.md5()
pass1.update(password)
pass2 = pass1.hexdigest()
return pass2[8:24]

def postLogin(url, user, password):
pass2 = getPass(password)
headers = {
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/51.0.2704.79 Safari/537.36 Edge/14.14393'}
post_data = {'username': user, 'password': pass2, 'drop': '0', 'type': '1', 'n': '100'}
post_data = urllib.urlencode(post_data)
req = urllib.Request(url, post_data, headers)
response = urllib.urlopen(req)

     #就是Post的内容了,当然密码是MD5加密过后的,这个随便找找也没有问题的

    #接下来是 检测网络是否连接,我采用Ping百度的办法,毕竟这是百度最大的功能之一,网上参考一下别人的代码

def ping(user, password):
fnull = open(os.devnull, 'w')
return1 = subprocess.call('ping 61.135.169.125', shell=True, stdout=fnull, stderr=fnull)
if return1:
print_ts('Internet not connected!')
print_ts('Connecting now...')
postLogin('http://159.226.39.22/cgi-bin/do_login', user, password)
ping(user, password)
else:
print_ts('Internet connected!')
fnull.close()

def print_ts(message):
print("[%s] %s" % (time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()), message))

# 定时运行,需要用到Python的time模块
def run(interval, user, password):
print_ts("-" * 50)
print_ts('Copyright (c) Huntley')
print_ts('Email:zhangzihao@ict.ac.cn')
print_ts('')
print_ts("Check the Internet every %s seconds." % interval)
print_ts("-" * 50)
while True:
try:
time_remaining = interval - time.time() % interval
time.sleep(time_remaining)
print_ts("Checking Internet connection...")
ping(user, password)
print_ts("Next check will start after %s seconds" % interval)
except Exception as e:
print(e)

if __name__ == '__main__':
# 设置联网检查时间间隔
# interval = 600
interval = 10
# 设置用户名
user = 'TYPE YOUR USERNAME HERE'
# 设置密码
password = 'TYPE YOUR PASSWORD HERE'
# 运行
run(interval, user, password)

Python 实现断网自动重连的更多相关文章

  1. Delphi使用ADO连接网络数据库,断网后重连问题

    原始文章: https://blog.csdn.net/blog_jihq/article/details/11737699# 使用TADOConnection对象连接网络数据库(以MySQL为例), ...

  2. Python离线断网情况下安装numpy、pandas和matplotlib等常用第三方包

    联网情况下在命令终端CMD中输入“pip install numpy”即可自动安装,pandas和matplotlib同理一样方法进行自动安装. 工作的电脑不能上外网,所以不能通过直接输入pip命令来 ...

  3. bat实现监测计算机网络连接,断网自动重启网络连接

    十月一体验了windows 10预览版之后,决定继续装回正式版,尝个鲜就好了,毕竟预览版还是不稳定,环境不是很方便. 决定装个最新正式版windows 8.1,结果问题来了,无线连接总是失败,显示网络 ...

  4. bat实现监测计算机无线连接,断网自动重启无线

    @echo off :Begin ping www.baidu.com if errorlevel 1 goto Reboot if errorlevel 0 goto Continue :Conti ...

  5. Python爬虫实战三之实现山东大学无线网络掉线自动重连

    综述 最近山大软件园校区QLSC_STU无线网掉线掉的厉害,连上之后平均十分钟左右掉线一次,很是让人心烦,还能不能愉快地上自习了?能忍吗?反正我是不能忍了,嗯,自己动手,丰衣足食!写个程序解决掉它! ...

  6. Delphi:ADOConnection连接SQLServer自动断网问题解决

    =============================== 解决方法一:异常时关闭连接,WinXP,win7 32位大部分情况都是起作用的,不过在有些windows操作系统下(如家庭版)不起作用, ...

  7. Win10电脑经常自动掉线、自动断网的解决方法

    近期一客户称自己使用电脑上网的时候,过一段时间莫名其妙的出现自动掉线.自动断网的情况,那么遇到这个问题该怎么办?下面装机之家分享一下Win10电脑经常自动掉线.自动断网的解决方法,以Win7系统为例. ...

  8. 断网环境,Python环境迁移

    Python刚装好断网了怎么办? wget不管用怎么办? 想把现有的Python环境转移到另外一台机器上怎么办? 这就用到了离线安装 1.创建目录用于存放下载的包,并进入该目录 mkdir lihai ...

  9. CSipIm断网重连崩溃原因分析

    断网重连之后的操作流程 拨打电话 ->调用SipService.makeCallOptions(),先重启一遍SipService保证PjSipService的各参数正常,尤其是create参数 ...

随机推荐

  1. 整理之Java容器

    主要参考:JAVA常见容器 Set,List,Map,Vector,ArrayList的区别 Java所有容器见下图: 数组 int[] t = new int[10]; //声明并创建长度为10的数 ...

  2. How to check type of files without extensions in python? 不通过文件扩展名,怎样知道文件类型?

    有一个命令 file 可以用 $ file fuck fuck.png: PNG image data, 1122 x 750, 8-bit colormap, non-interlaced pyth ...

  3. Mysql常用sql语句(10)- is null 空值查询

    测试必备的Mysql常用sql语句系列 https://www.cnblogs.com/poloyy/category/1683347.html 前言 is null是一个关键字来的,用于判断字段的值 ...

  4. Hive的分桶表

    [分桶概述] Hive表分区的实质是分目录(将超大表的数据按指定标准细分到指定目录),且分区的字段不属于Hive表中存在的字段:分桶的实质是分文件(将超大文件的数据按指定标准细分到分桶文件),且分桶的 ...

  5. Intel® QAT加速卡之Linux上编程说明

    QAT Software for Linux 1. Introduction 该程序员指南提供了有关软件体系结构和使用指南的信息. 相关的英特尔QAT软件库文档中记录了有关使用英特尔QuickAssi ...

  6. DHCP 协议及其交互过程

    1. DHCP用途简介: DHCP服务应用于大型局域网络中,使网络中的主机自动获取IP地址,网关,DNS服务器等信息,能够提升IP地址的利用率.一般情况下,我们的家用.公司.公共场合使用的路由器都具有 ...

  7. C# AutoMaper简单使用

    AutoMapper它是一种对象与对象之间的映射器,让AutoMapper有意思的就是在于它提供了一些将类型A映射到类型B这种无聊的实例,只要B遵循AutoMapper已经建立的惯例,那么大多数情况下 ...

  8. Mybatis log plugin插件破解修复版 MyBatis Log Plugin License Authorization Failed

    github地址 - https://github.com/Link-Kou/intellij-mybaitslog

  9. php时间区间,优化显示

    <?php /** * 类似微信的时间显示 * 规则是:今天的,显示几秒前,几分钟前,几小时前,昨天的显示昨天 上午 XX:XX * 再往前,本周的,显示周几+时间,再往前,本年的,显示月日+时 ...

  10. 织梦 arclist调用副栏目内容解决办法

    1 打开include/taglib/arclist.lib.php,找到296行: if($CrossID=='') $orwheres[] = ' arc.typeid IN ('.GetSonI ...