# -*- coding:utf-8 -*-
import urllib
import urllib2
import socket
import types
import time
import os
import subprocess
class Login:
# 初始化
def __init__(self):
self.username = '3331*S******'
self.password = '******'
self.ip_pre = '219.223'
# 检测间隔时间,单位为秒
self.every = 5*60 # second
# 模拟登录
def login(self):
print self.getCurrentTime(), u"正在尝试认证UTSZ无线网络........"
data = {
'action':'login',
"username": self.username,
"password": self.password,
"ac_id": 1,
"user_ip:": "",
"nas_ip": "",
"user_mac": "",
"save_me": 1,
"ajax": 1,
}
# 消息头
headers = {
'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; WOW64) '
'AppleWebKit/537.36 (KHTML, like Gecko) '
'Chrome/45.0.2454.101 Safari/537.36',
'Host': '10.0.10.66',
'Origin': 'http://10.0.10.66',
'Referer': 'http://10.0.10.66/srun_portal_pc.php?ac_id=1&url='
}
post_data = urllib.urlencode(data)
login_url = "http://10.0.10.66/include/auth_action.php"
request = urllib2.Request(login_url, post_data, headers)
response = urllib2.urlopen(request)
result = response.read() # .decode('gbk')
self.getLoginResult(result)
# 打印登录结果
def getLoginResult(self, result):
if "login_ok" in result:
print self.getCurrentTime(), 'login_ok'
else:
print self.getCurrentTime(), result
# 获取本机无线IP
def getIP(self):
local_iP = socket.gethostbyname(socket.gethostname())
if self.ip_pre in str(local_iP):
return str(local_iP)
ip_lists = socket.gethostbyname_ex(socket.gethostname())
for ip_list in ip_lists:
if isinstance(ip_list, list):
for i in ip_list:
if self.ip_pre in str(i):
return str(i)
elif type(ip_list) is types.StringType:
if self.ip_pre in ip_list:
return ip_list
# 判断当前是否可以联网
def canConnect(self):
fnull = open(os.devnull, 'w')
result = subprocess.call('ping www.baidu.com', shell=True, stdout=fnull, stderr=fnull)
fnull.close()
if result:
return False
else:
return True
# 获取当前时间
def getCurrentTime(self):
return time.strftime('[%Y-%m-%d %H:%M:%S]', time.localtime(time.time()))
# 主函数
def main(self):
print self.getCurrentTime(), u"您好,欢迎使用模拟登陆系统"
while True:
nowIP = self.getIP()
if not nowIP:
print self.getCurrentTime(), u"请检查是否正常连接UTSZ无线网络"
else:
print self.getCurrentTime(), u"成功连接了UTSZ网络,本机IP为", nowIP
self.login()
while True:
if not self.canConnect():
if not self.getIP():
print self.getCurrentTime(), u"当前已经掉线,请确保连接上了UTSZ网络"
else:
print self.getCurrentTime(), u"当前已经掉线,正在尝试重新连接"
self.login()
else:
print self.getCurrentTime(), u"当前网络连接正常"
time.sleep(self.every)
time.sleep(self.every)
login = Login()
login.main()
【参考资料】
网友评论