这几天研究了下12306分流,之前12306pass不停切换CDN来刷新数据,这样减少缓存时间,写了个脚本从官网上抓了一批CDN服务器来测试
验证cdn脚本:
#! /usr/bin/env python3
# -*- coding:utf-8 -*-
import os,requests,time,re
import sys,codecs,json
requests.packages.urllib3.disable_warnings(requests.packages.urllib3.exceptions.InsecureRequestWarning)
#self.session = requests.session()
headers = {
'Host': 'kyfw.12306.cn',
'Origin': 'https://kyfw.12306.cn',
'X-Requested-With': 'XMLHttpRequest',
'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8',
'Referer': 'https://kyfw.12306.cn/otn/login/init',
'Accept': '*/*',
'Accept-Encoding': 'gzip, deflate, br',
'Accept-Language': 'zh-CN,zh;q=0.8',
'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/47.0.2526.73 Safari/537.36',
}
#读取iptxt文件
def loadtxt():
ysyurl = "{0}/iprange.txt".format(sys.path[0])
f = open(ysyurl) # 返回一个文件对象
realips = []
line = f.readline() # 调用文件的 readline()方法
while line:
line = f.readline()
line = re.sub('\n', '', line)
realips.append(line)
f.close()
return realips
realips=loadtxt()
curnum=0
for ip in realips:
try:
url="https://{0}/otn/".format(ip)
print(url)
response = requests.get(url=url,headers=headers, timeout=5, verify=False)
print(response.headers)
if (response.status_code==200):
curnum += 1
print(curnum)
time.sleep(1.5)
except Exception as e:
print(e)
目前基本实现了12306pass的功能,判断了下,有时候可能链接超时会自动转换CDN