首页 > 编程 > Python > 正文

Python获取Shadowsocks账号

2019-11-10 16:47:52
字体:
来源:转载
供稿:网友

  Shadowsocks(中文名称:影梭)是使用Python、C++、C#等语言开发、基于Apache许可证的开放源代码软件,用于保护网络流量、加密数据传输。可以用来访问Google等网站。   本文介绍一种用Python3从freevpnss网站获取ss账号的方法。

所需工具

Python3shadowsocks-win

从网页获取ss账号

  爬取freevpnss网站上的账号和密码存到list中。

import reimport urllib.requestURL = 'https://freevpnss.cc/'# SS accountSERVER = []SERVER_PORT = []PASSWord = []METHOD = []REMARKS = []def get_data(): ''' Get ss account ''' page = urllib.request.urlopen(URL) html = page.read().decode('utf-8') # SERVER datas = re.findall(re.compile(r'<p>服务器地址:(.+)</p>'), html) SERVER.clear() for data in datas: if data not in SERVER: SERVER.append(data) # SERVER_PORT datas = re.findall(re.compile(r'<p>端口:(.+)</p>'), html) SERVER_PORT.clear() for data in datas: SERVER_PORT.append(data) # PASSWORD datas = re.findall(re.compile( r'<p>密<span class="hidden">.</span>码:(.+)</p>'), html) PASSWORD.clear() for data in datas: if data not in PASSWORD: PASSWORD.append(data) # METHOD datas = re.findall(re.compile(r'<p>加密方式:(.+)</p>'), html) METHOD.clear() for data in datas: METHOD.append(str.lower(data)) # REMARKS REMARKS.clear() for server in SERVER: remark = str.split(server, '.')[0] REMARKS.append(str.upper(remark))

将获取的ss账号信息保存

  Shadowsocks的Windows版客户端将配置信息存放到gui-config.json文件中,我们需要将上面获取的账号信息存放到这个文件中。   gui-config.json文件的结构如下:

{ "configs": [ { "server": "", "server_port": "", "password": "", "method": "", "remarks": "", "auth": false, "timeout": 5 } ], "strategy": "com.shadowsocks.strategy.balancing", "index": -1, "global": false, "enabled": true, "shareOverLan": false, "isDefault": false, "localPort": 1080, "pacUrl": null, "uSEOnlinePac": false, "secureLocalPac": true, "availabilityStatistics": false, "autoCheckUpdate": true, "checkPReRelease": false, "isVerboseLogging": false, "logViewer": { "topMost": false, "wrapText": false, "toolbarShown": false, "Font": "Consolas, 8pt", "BackgroundColor": "Black", "TextColor": "White" }, "proxy": { "useProxy": false, "proxyType": 0, "proxyServer": "", "proxyPort": 0, "proxyTimeout": 3 }, "hotkey": { "SwitchSystemProxy": "", "SwitchSystemProxyMode": "", "SwitchAllowLan": "", "ShowLogs": "", "ServerMoveUp": "", "ServerMoveDown": "" }}

  我们只需要将获取的账号信息存放到文件的config数组中即可。

FILE_PATH = 'gui-config.json'def write_to_file(): ''' Write server info to file''' with open(FILE_PATH, 'wt') as f: print('{', file=f) # configs print(r' "configs": [', file=f) for i in range(3): print(r' {', file=f) print(r' "server": "' + SERVER[i] + r'",', file=f) print(r' "server_port": "' + SERVER_PORT[i] + r'",', file=f) print(r' "password": "' + PASSWORD[i] + r'",', file=f) print(r' "method": "' + METHOD[i] + r'",', file=f) print(r' "remarks": "' + REMARKS[i] + r'",', file=f) print(r' "auth": false,', file=f) print(r' "timeout": 5', file=f) if i != 2: print(r' },', file=f) else: print(r' }', file=f) print(r' ],', file=f) # other print(r''' "strategy": "com.shadowsocks.strategy.balancing", "index": -1, "global": false, "enabled": true, "shareOverLan": false, "isDefault": false, "localPort": 1080, "pacUrl": null, "useOnlinePac": false, "secureLocalPac": true, "availabilityStatistics": false, "autoCheckUpdate": true, "checkPreRelease": false, "isVerboseLogging": false, "logViewer": { "topMost": false, "wrapText": false, "toolbarShown": false, "Font": "Consolas, 8pt", "BackgroundColor": "Black", "TextColor": "White" }, "proxy": { "useProxy": false, "proxyType": 0, "proxyServer": "", "proxyPort": 0, "proxyTimeout": 3 }, "hotkey": { "SwitchSystemProxy": "", "SwitchSystemProxyMode": "", "SwitchAllowLan": "", "ShowLogs": "", "ServerMoveUp": "", "ServerMoveDown": "" }''', file=f) print('}', file=f) f.close()

主函数

  到这里ss账号的获取和写入文件已经完成了,接下来只需要运行Shadowsocks.exe即可。   这里可以定义一个主函数main来将所有功能整合起来。

def main(): ''' Main function ''' get_data() write_to_file()if __name__ == '__main__': main()
发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表