Python 实现断网自动重连
程序员文章站
2023-12-21 18:29:28
...
先从最基本的功能说起:发送Post给服务器。这里你需要知道你登录的时候到底发送了什么,大概看一下登录页面的代码:
function do_login()
{
var uname=document.form1.uname.value;
var pass=document.form1.pass.value;
if(uname=="")
{
alert("请填写用户名");
document.form1.uname.focus();
return;
}
if(pass=="")
{
alert("请填写密码");
document.form1.pass.focus();
return;
}
//密码md5加密传送
var pass1=hex_md5(pass);
var pass2=pass1.substr(8,16);
var drop=(document.form1.drop.checked==true)?1:0;
var data="username="+uname+"&password="+pass2+"&drop="+drop+"&type=1&n=100";
var con=postData("/cgi-bin/do_login", "post", data);
//alert(con);
var p=/^[\d]+$/;
if(p.test(con))
{
document.form1.uid.value=con;
if(document.form1.save_me.checked) //写COOKIE
{
setCookie("srun_login",uname+"|"+pass);
}
else
{
delCookie("srun_login");
}
if(location.search != "")
{
window.open("login.html"+location.search+","+con+","+uname,"user_login","width=428,height=296");
}
else
{
window.open("login.html?"+location.search+","+con+","+uname,"user_login","width=428,height=296");
}
setTimeout("jump_to(url)", 1000);
return;
}
switch(con)
{
}
}
可以发现,
var data="username="+uname+"&password="+pass2+"&drop="+drop+"&type=1&n=100";
这一句,就是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()
最后是定时运行,需要用到Python的time模块
def print_ts(message):
print "[%s] %s"%(time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()), message)
def run(interval,user,password):
print_ts("-"*50)
print_ts ('Copyright (c) Huntley')
print_ts ('Email:[email protected]')
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, e:
print e
完整代码:
#!/usr/bin/env python
#coding=utf8
import urllib
import urllib2
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 = urllib2.Request(url, post_data, headers)
response = urllib2.urlopen(req)
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)
def run(interval,user,password):
print_ts("-"*50)
print_ts ('Copyright (c) Huntley')
print_ts ('Email:[email protected]')
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, e:
print e
# 设置联网检查时间间隔
interval = 600
# 设置用户名
user = 'TYPE YOUR USERNAME HERE'
# 设置密码
password = 'TYPE YOUR PASSWORD HERE'
# 运行
run(interval,user,password)