• Python实现自动登录/登出校园网网关


        学校校园网的网络连接有免费连接和收费连接两种类型,可想而知收费连接浏览体验更佳,比如可以访问更多的网站。之前收费地址只能开通包月服务才可使用,后来居然有了每个月60小时的免费使用收费地址的优惠。但是,一旦连接了收费地址而忘记及时断开,60小时会很快用完。 
        为了节约收费地址的使用时间,采用如下方案:每隔1个小时,都在本机上断开校园网的收费连接,同时连接免费连接,这样,每次手动连接收费连接之后,最多使用1个小时,就会被自动断开。

    1. python实现连接/断开网络连接 
         通过执行python脚本,实现访问 its.pku.edu.cn 并设置连接/断开。 
    通过fiddler抓包,分析从访问 its.pku.edu.cn,到填写账号密码,再到最后登陆成功的过程中,浏览器和网关交互的http request和response。 

    python代码实现 

    #!/usr/bin/env python
    """python crawler """
    #encoding:UTF-8
    
    import requests
    import sys
    
    
    def login(type):
        """login work"""
        headers =  {'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8',
             'Accept-Encoding': 'gzip, deflate, sdch, br',
             'Accept-Language': 'zh-CN,zh;q=0.8',
             'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/53.0.2785.101 Safari/537.36'}
        s = requests.Session()
        s.headers = headers
        post_data = {'username': 'username', 'password': 'password', 'iprange': type}
        r = s.post('https://its.pku.edu.cn/cas/webLogin', data=post_data, verify=False)
        print "log in, status = %s" % r.status_code
    
    
    def logout(type):
        """logout work"""
        headers =  {'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8',
             'Accept-Encoding': 'gzip, deflate, sdch, br',
             'Accept-Language': 'zh-CN,zh;q=0.8',
             'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/53.0.2785.101 Safari/537.36'}
        s = requests.Session()
        s.headers = headers
        post_data = {'username': 'username', 'password': 'password', 'iprange': 'no'}
        s.post('https://its.pku.edu.cn/cas/webLogin', data=post_data, verify=False)
        #to logout!!
        r = s.get('https://its.pku.edu.cn/netportal/ITSipgw?cmd=close&type=%s&sid=478' % type, verify=False, headers=headers)
        print "log out, status = %s" % r.status_code
    
    
    if __name__ == '__main__':
        if len(sys.argv) != 2:
            print "usage <option: 1 (connect free), 2 (connect global), 3 (disconnect this computer), 4 (disconnect all), 5(disconnect this computer and connect free)"
            exit(1)
    
        option = int(sys.argv[1])
        if option == 1:
            print "try to connect free"
            login('no')
        elif option == 2:
            print "try to connect global"
            login('yes')
        elif option == 3:
            print "try to disconnect self"
            logout('self')
        elif option == 4:
            print "try to disconnect all"
            logout('all')
    

    2. 每1小时自动执行 
        linux下比较方便,直接设置 crontab即可。 
        windows下可以设置机器的自动任务计划,具体步骤如下: 
    (1)开始->所有工具->windows管理工具->任务计划程序 
    (2)创建任务 
    (2.1)常规 ——填写程序名称(随意) 
    (2.2)触发器 ——新建,设置为每天固定时间开始执行,每隔一个小时重复一次 

    (2.3)操作 ——新建,设置为执行python脚本,且设置正确的命令行参数 (包括python脚本路径和参数5)

  • 相关阅读:
    touch测试
    JS动画代码
    前端css、javascript在线工具
    横向广告(商品)滚动
    写点js的小函数(一)
    HTML5 css reset
    JS新API标准 地理定位(navigator.geolocation)
    写点js的小函数(二、文本框的提示)
    传说中的comet(ajax版)?
    lhgdialog 4.2.0 正式版发布
  • 原文地址:https://www.cnblogs.com/gtarcoder/p/4903253.html
Copyright © 2020-2023  润新知