{python} 写小段代码翻墙

某个网站提供免费的SS服务器,不过隔几分钟就会更换密码。本意就是让用户体验下服务,之后来包月。本来就想着探索下怎么用python来写个爬网站,正好试试怎么自动抓到密码,避免每次都手动操作。不是要钻人家空子,只是好玩而已,我还是很乐意付费的。
做法很简单,该网站页面既显示密码,又提供gui-config下载,拿到之后替换本地的config,自动重启下SS即可。

代码:

#update ss with free bw node
import urllib.request, urllib.parse
import os.path
import json
import sched
import time

schedule = sched.scheduler ( time.time, time.sleep )  

def getLoginCookies():

    cookie = "本地"
    return cookie

def getPage(url, headers):
    req = urllib.request.Request(url, headers=headers)
    response = urllib.request.urlopen(req)
    html_bytes = response.read()
    html_string = html_bytes.decode('utf-8')
    return html_string,html_bytes

def getHttpHeaders(cookie):
    headers = {}
    headers['Cookie'] = cookie
    headers['User-Agent']='Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2272.101 Safari/537.36'
    return headers

def getJsonHeaders(cookie):
    headers = getHttpHeaders(cookie)
    headers['Content-Type']='application/json'
    return headers
    
def getFreeBwNodePage(cookie):
    headers = getJsonHeaders(cookie)
    url = 'https://www.我不写出来.com/ucenter/?act=free_nodes_export'
    content = getPage(url, headers)
    return content

def modify_free_config(config,dest):
    x0 = json.loads(config)
    destfile = os.path.join(dest, 'gui-config.json')
    
    f = open(destfile, 'rt')
    x1 = json.loads(f.read())
    f.close()
    x1['configs'][0]['password'] = x0['configs'][0]['password']
    print('NewPassword:{0}'.format(x0['configs'][0]['password']))

    f = open(destfile, 'wt')
    f.write(json.dumps(x1))
    f.close()

def stop_program(procname):
    os.system('TASKKILL /F /IM {0}*'.format(procname))

def start_program(progpath):
    os.startfile(progpath)  

def refreshSS():
    ssroot = r'sspath'
    ssname = 'ssname'
    cookie = getLoginCookies()
    print('get cookie:{0}'.format(cookie))
    
    content = getFreeBwNodePage(cookie)
    print('get bw node:{0}'.format(content[0]))
    
    stop_program('Shadow')
    modify_free_config(content[0], ssroot)
    start_program(os.path.join(ssroot,ssname))
    print('SS config refreshed')

def scheduledRefreshSS():
    M1 = 60
    M3 = M1 * 3
    schedule.enter(M3,0,refreshSS)
    schedule.run()

if __name__ == '__main__':
    refreshSS()
    scheduledRefreshSS()
    

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 136,124评论 19 139
  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 176,355评论 25 709
  • Ubuntu的发音 Ubuntu,源于非洲祖鲁人和科萨人的语言,发作 oo-boon-too 的音。了解发音是有意...
    萤火虫de梦阅读 100,147评论 9 468
  • ¥开启¥ 【iAPP实现进入界面执行逐一显】 〖2017-08-25 15:22:14〗 《//首先开一个线程,因...
    小菜c阅读 11,887评论 0 17
  • 这个月相对上个月来说,写得比较轻松,因为周末两天可以休息一下,整理一下。还是练习阶段,还是漫无目的的有感而发,随手...
    茜茜妈妈阅读 1,328评论 0 1

友情链接更多精彩内容