[size=11]
自己写了一个访问网站,自动更新服务器密码的python脚本,虽然脚本的整体思路比较简单。但是随着写程序发现了很多自己想当然的问题,但是结果却不是那样。虽然最后大部分都通过网络解决了,防止将来自己还会遇到,所以在此记录备忘。
[/size]
自己写了一个访问网站,自动更新服务器密码的python脚本,虽然脚本的整体思路比较简单。但是随着写程序发现了很多自己想当然的问题,但是结果却不是那样。虽然最后大部分都通过网络解决了,防止将来自己还会遇到,所以在此记录备忘。
[/size]
#coding=utf-8
#Filename: updateserver.py
#date: 2016-3-24
#python: python 2.7.10
import re, urllib2
# 网址和网页内容的抓取
url = 'http://www.ishadowsocks.net/'
content = urllib2.urlopen(url).read()
# 服务器的正则设定和提取
server_re = '[a-z0-9]{3}\.[a-z]{3}\.tf'
server_list = re.findall(server_re, content)
# 端口的正则设定和提取
port_re = ':[0-9]{2,5}<'
port_list_orig = re.findall(port_re, content)
port_list = [x[1:-1] for x in port_list_orig]
# 密码的正则设定和提取
password_re = ':[0-9]{8}<'
password_list_orig = re.findall(password_re, content)
password_list = [x[1:-1] for x in password_list_orig]
# 挑选合适的服务器,以香港为主,相对较快,国外的最后选择
for i in range(0, 3):
servername = server_list[i]
port = port_list[i]
pw = password_list[i]
if 'hk' in servername:
break
else:
continue
# 取得python的工作目录
#work_dir = os.getcwd()
#print work_dir
# 读取文件内容并更改服务器、端口号、密码
with open(r'D:\Program Files\Shadowsocks-3.0\gui-config.json', 'r') as f:
lines = f.readlines()
lines[3] = '"server" : "%s",\n' % servername
lines[4] = '"server_port" : %s,\n' %port
lines[5] = '"password" : "%s",\n' % pw
# 写入新的服务器和密码
with open(r'D:\Program Files\Shadowsocks-3.0\gui-config.json', 'w') as f:
f.writelines(lines)