From 0bb941364b9f831bcfb7026d80f087ef102cf276 Mon Sep 17 00:00:00 2001 From: "xianping.wen" <931128603@qq.com> Date: Thu, 12 Sep 2019 12:34:51 +0800 Subject: [PATCH] update version 1.1.112 --- TickerConfig.py | 5 ++++- UnitTest/TestAll.py | 10 ++++++++++ myUrllib/httpUtils.py | 23 +++++++++++++++++------ 3 files changed, 31 insertions(+), 7 deletions(-) diff --git a/TickerConfig.py b/TickerConfig.py index 013792f..9232d1b 100644 --- a/TickerConfig.py +++ b/TickerConfig.py @@ -114,6 +114,9 @@ COOKIE_TYPE = 1 # chromedriver配置版本只要和chrome的大版本匹配就行 CHROME_PATH = "/Users/wenxianping/Downloads/chromedriver" +# 1=>为一直随机ua,2->只启动的时候随机一次ua +RANDOM_AGENT = 2 + PASSENGER_TICKER_STR = { '一等座': 'M', '特等座': 'P', @@ -127,4 +130,4 @@ PASSENGER_TICKER_STR = { } # 软件版本 -RE_VERSION = "1.1.111" +RE_VERSION = "1.1.112" diff --git a/UnitTest/TestAll.py b/UnitTest/TestAll.py index 4483d3f..5f543ef 100644 --- a/UnitTest/TestAll.py +++ b/UnitTest/TestAll.py @@ -53,6 +53,16 @@ class testAll(unittest.TestCase): """ sendServerChan(u"server酱 微信通知测试一下") + def testUserAgent(self): + """ + 测试UserAgent + :return: + """ + from fake_useragent import UserAgent + for i in range(10000): + ua = UserAgent(verify_ssl=False) + print(ua.random) + if __name__ == '__main__': unittest.main() \ No newline at end of file diff --git a/myUrllib/httpUtils.py b/myUrllib/httpUtils.py index e234926..5403d61 100755 --- a/myUrllib/httpUtils.py +++ b/myUrllib/httpUtils.py @@ -1,11 +1,11 @@ # -*- coding: utf8 -*- import json -import random import socket from collections import OrderedDict from time import sleep import requests - +from fake_useragent import UserAgent +import TickerConfig from agency.agency_tools import proxy from config import logger @@ -15,15 +15,18 @@ def _set_header_default(): # header_dict["Accept"] = "application/json, text/plain, */*" header_dict["Accept-Encoding"] = "gzip, deflate" header_dict[ - "User-Agent"] = "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/75.0." + str( - random.randint( - 5000, 7000)) + ".0 Safari/537.36" + "User-Agent"] = _set_user_agent() header_dict["Content-Type"] = "application/x-www-form-urlencoded; charset=UTF-8" header_dict["Origin"] = "https://kyfw.12306.cn" header_dict["Connection"] = "keep-alive" return header_dict +def _set_user_agent(): + user_agent = UserAgent(verify_ssl=False).random + return user_agent + + class HTTPClient(object): def __init__(self, is_proxy): @@ -90,6 +93,12 @@ class HTTPClient(object): self._s.headers.update({"Host": host}) return self + def setHeadersUserAgent(self): + self._s.headers.update({"User-Agent": _set_user_agent()}) + + def getHeadersUserAgent(self): + return self._s.headers["User-Agent"] + def getHeadersReferer(self): return self._s.headers["Referer"] @@ -121,7 +130,9 @@ class HTTPClient(object): else: method = "get" self.resetHeaders() - self.setHeadersReferer(urls["Referer"]) + if TickerConfig.RANDOM_AGENT is 1: + self.setHeadersReferer(urls["Referer"]) + self.setHeadersUserAgent() if is_logger: logger.log( u"url: {0}\n入参: {1}\n请求方式: {2}\n".format(req_url, data, method, ))