python自动拨号,之前写某个项目需要用自动拨号。市面上的拨号老是错误,也不太好对接python,就自己写了下模块来对接程序。需要的可以直接复制粘贴使用。
#!/usr/bin/python3
# -*- coding: utf-8 -*-
import time, os
from datetime import datetime
import requests
import random
class ADSL(object):
def __init__(self,):
self.name = '宽带连接'
self.username = '2021SS10008'
self.password = '97882732'
def set_adsl(self, account):
self.name = account["name"]
self.username = account["username"]
self.password = account["password"]
def connect(self):
cmd_str = f"rasdial {self.name} {self.username} {self.password}"
os.system(cmd_str)
time.sleep(2)
def disconnect(self):
cmd_str = f"rasdial {self.name} /disconnect"
os.system(cmd_str)
time.sleep(1)
def check(self):
headers = {
'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.132 Safari/537.36'
}
urls=['https://www.qq.com','https://www.baidu.com','https://www.jb51.net/','https://www.aizhan.com/','https://www.taobao.com',
'https://www.sohu.com/',
'https://www.iqiyi.com/',
'https://youku.com/',
'https://www.autohome.com.cn/',
'https://www.sogou.com/',
]
url=random.choice(urls)
try:
req = requests.get(url,headers=headers)
restat = req.elapsed.total_seconds()
#print(req.text)
except Exception as e:
return 20 ###网络错误返回参数给递归函数重连宽带
return restat
def reconnect(self):
self.disconnect()
self.connect()
if self.check()< 10:
print('拨号成功')
return'拨号成功'
print('网络连接失败,正在重连')
return self.reconnect()
if __name__ == '__main__':
#for i in range(99):
adsl =ADSL()
print(adsl.reconnect())