You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
12306/agency/cdn_utils.py

89 lines
2.9 KiB

7 years ago
# encoding=utf8
7 years ago
import collections
7 years ago
import json
import os
7 years ago
import re
7 years ago
import sys
import csv
7 years ago
import requests
from config import urlConf
try:
6 years ago
reload(sys)
sys.setdefaultencoding('utf-8')
except NameError:
pass
7 years ago
7 years ago
class CDNProxy:
def __init__(self, host=None):
self.host = host
self.urlConf = urlConf.urls
self.httpClint = requests
self.city_list = []
self.timeout = 5
def _set_header(self):
"""设置header"""
return {
"Content-Type": "application/x-www-form-urlencoded; charset=utf-8",
"X-Requested-With": "xmlHttpRequest",
"User-Agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.132 Safari/537.36",
"Referer": "https://kyfw.12306.cn/otn/login/init",
"Accept": "*/*",
}
def get_city_id(self):
"""
获取所有城市md5参数
:return:
"""
try:
if self.host:
while True:
url = self.urlConf["cdn_host"]["req_url"]
data = {"host": self.host, "lintType": "电信,多线,联通,移动"}
rep = self.httpClint.post(url, data, headers=self._set_header(), timeout=self.timeout)
city_re = re.compile(r"<li id=\"(\S+)\" class=\"PingListCent PingRLlist")
self.city_list = re.findall(city_re, rep.content)
if self.city_list:
6 years ago
print(self.city_list)
7 years ago
break
else:
pass
except:
pass
def open_cdn_file(self):
cdn = []
6 years ago
# cdn_re = re.compile("CONNECT (\S+) HTTP/1.1")
# path = os.path.join(os.path.dirname(__file__), '../cdn_list')
# with open(path, "r") as f:
# for i in f.readlines():
# # print(i.replace("\n", ""))
# cdn_list = re.findall(cdn_re, i)
# if cdn_list and "kyfw.12306.cn:443" not in cdn_list:
# print(cdn_list[0].split(":")[0])
# cdn.append(cdn_list[0].split(":")[0])
# return cdn
path = os.path.join(os.path.dirname(__file__), '../cdn_list')
try:
with open(path, "r", encoding="utf-8") as f:
for i in f.readlines():
# print(i.replace("\n", ""))
if i and "kyfw.12306.cn:443" not in i:
cdn.append(i.replace("\n", ""))
return cdn
6 years ago
except Exception:
with open(path, "r") as f:
for i in f.readlines():
# print(i.replace("\n", ""))
if i and "kyfw.12306.cn:443" not in i:
cdn.append(i.replace("\n", ""))
return cdn
7 years ago
if __name__ == '__main__':
cdn = CDNProxy()
print(cdn.open_cdn_file())