From fa85652febd8e7a58dfedee82aa6dc35aefbc238 Mon Sep 17 00:00:00 2001 From: wenxianping <931128603@qq.com> Date: Fri, 4 Jan 2019 09:38:40 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96=E7=99=BB=E5=BD=95=E6=8E=89?= =?UTF-8?q?=E7=BA=BF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- agency/cdn_utils.py | 2 +- config/urlConf.py | 1 + init/select_ticket_info.py | 31 ++++++++++++++++--------------- inter/CheckUser.py | 31 ++++++++++++++++--------------- myUrllib/httpUtils.py | 9 ++++++--- 5 files changed, 40 insertions(+), 34 deletions(-) diff --git a/agency/cdn_utils.py b/agency/cdn_utils.py index dad4dbe..7626f6f 100755 --- a/agency/cdn_utils.py +++ b/agency/cdn_utils.py @@ -91,7 +91,7 @@ class CDNProxy: f.write(json.dumps(local_dict)+"\n") num += 1 except Exception as e: - print(e.message) + print(e) print(u"本次cdn获取完成,总个数{0}".format(num)) def all_cdn(self): diff --git a/config/urlConf.py b/config/urlConf.py index 9599c89..2963815 100755 --- a/config/urlConf.py +++ b/config/urlConf.py @@ -53,6 +53,7 @@ urls = { "s_time": 0.1, "is_logger": False, "is_json": False, + "not_decode": True, }, "codeCheck": { # 验证码校验 "req_url": "/passport/captcha/captcha-check", diff --git a/init/select_ticket_info.py b/init/select_ticket_info.py index e6ee5cf..054cc87 100755 --- a/init/select_ticket_info.py +++ b/init/select_ticket_info.py @@ -62,11 +62,11 @@ class select: :return: """ ticket_info_config = _get_yaml() - from_station = ticket_info_config["set"]["from_station"].encode("utf8") - to_station = ticket_info_config["set"]["to_station"].encode("utf8") + from_station = ticket_info_config["set"]["from_station"] + to_station = ticket_info_config["set"]["to_station"] station_dates = ticket_info_config["set"]["station_dates"] set_names = ticket_info_config["set"]["set_type"] - set_type = [seat_conf[x.encode("utf8")] for x in ticket_info_config["set"]["set_type"]] + set_type = [seat_conf[x.encode("utf-8")] for x in ticket_info_config["set"]["set_type"]] is_more_ticket = ticket_info_config["set"]["is_more_ticket"] ticke_peoples = ticket_info_config["set"]["ticke_peoples"] station_trains = ticket_info_config["set"]["station_trains"] @@ -179,13 +179,14 @@ class select: l.reqLiftTicketInit() self.call_login() check_user = checkUser(self) - check_user.sendCheckUser() + t = threading.Thread(target=check_user.sendCheckUser) + t.setDaemon(True) + t.start() from_station, to_station = self.station_table(self.from_station, self.to_station) num = 0 while 1: try: num += 1 - check_user.sendCheckUser() now = datetime.datetime.now() # 感谢群里大佬提供整点代码 if now.hour >= 23 or now.hour < 6: print(u"12306休息时间,本程序自动停止,明天早上七点将自动运行") @@ -277,31 +278,31 @@ class select: random_time)) time.sleep(random_time) except PassengerUserException as e: - print(e.message) + print(e) break except ticketConfigException as e: - print(e.message) + print(e) break except ticketIsExitsException as e: - print(e.message) + print(e) break except ticketNumOutException as e: - print(e.message) + print(e) break except UserPasswordException as e: - print(e.message) + print(e) break except ValueError as e: - if e.message == "No JSON object could be decoded": + if e == "No JSON object could be decoded": print(u"12306接口无响应,正在重试") else: - print(e.message) + print(e) except KeyError as e: - print(e.message) + print(e) except TypeError as e: - print(u"12306接口无响应,正在重试 {0}".format(e.message)) + print(u"12306接口无响应,正在重试 {0}".format(e)) except socket.error as e: - print(e.message) + print(e) if __name__ == '__main__': diff --git a/inter/CheckUser.py b/inter/CheckUser.py index 7ababa0..1cfd28e 100644 --- a/inter/CheckUser.py +++ b/inter/CheckUser.py @@ -15,20 +15,21 @@ class checkUser: :return: """ CHENK_TIME = 0.3 - if wrapcache.get("user_time") is None: - check_user_url = self.session.urls["check_user_url"] - data = {"_json_att": ""} - check_user = self.session.httpClint.send(check_user_url, data) - if check_user.get("data", False): - check_user_flag = check_user["data"]["flag"] - if check_user_flag is True: - wrapcache.set("user_time", datetime.datetime.now(), timeout=60 * CHENK_TIME) - else: - if check_user['messages']: - print (ticket.LOGIN_SESSION_FAIL.format(check_user['messages'])) - self.session.call_login() + while 1: + if wrapcache.get("user_time") is None: + check_user_url = self.session.urls["check_user_url"] + data = {"_json_att": ""} + check_user = self.session.httpClint.send(check_user_url, data) + if check_user.get("data", False): + check_user_flag = check_user["data"]["flag"] + if check_user_flag is True: wrapcache.set("user_time", datetime.datetime.now(), timeout=60 * CHENK_TIME) else: - print (ticket.LOGIN_SESSION_FAIL.format(check_user['messages'])) - self.session.call_login() - wrapcache.set("user_time", datetime.datetime.now(), timeout=60 * CHENK_TIME) \ No newline at end of file + if check_user['messages']: + print (ticket.LOGIN_SESSION_FAIL.format(check_user['messages'])) + self.session.call_login() + wrapcache.set("user_time", datetime.datetime.now(), timeout=60 * CHENK_TIME) + else: + print (ticket.LOGIN_SESSION_FAIL.format(check_user['messages'])) + self.session.call_login() + wrapcache.set("user_time", datetime.datetime.now(), timeout=60 * CHENK_TIME) \ No newline at end of file diff --git a/myUrllib/httpUtils.py b/myUrllib/httpUtils.py index c1e1ad8..5712981 100755 --- a/myUrllib/httpUtils.py +++ b/myUrllib/httpUtils.py @@ -5,8 +5,6 @@ from collections import OrderedDict from time import sleep import requests from config import logger -import wrapcache - def _set_header_default(): header_dict = OrderedDict() @@ -137,11 +135,16 @@ class HTTPClient(object): verify=False, **kwargs) if response.status_code == 200 or response.status_code == 302: + if urls.get("not_decode", False): + return response.content if response.content: if is_logger: logger.log( u"出参:{0}".format(response.content)) - return json.loads(response.content) if urls["is_json"] else response.content + if urls["is_json"]: + return json.loads(response.content.decode() if isinstance(response.content, bytes) else response.content) + else: + return response.content.decode("utf8", "ignore") if isinstance(response.content, bytes) else response.content else: logger.log( u"url: {} 返回参数为空".format(urls["req_url"]))