增加代理ip口子

pull/128/head
wenxianping 2019-01-08 17:04:40 +08:00
parent 8e97d1d160
commit ad8066972b
15 changed files with 120 additions and 47 deletions

View File

@ -35,7 +35,8 @@
![image](https://github.com/testerSunshine/12306/blob/master/uml/uml.png) ![image](https://github.com/testerSunshine/12306/blob/master/uml/uml.png)
- 项目声明: - 项目声明:
- 本软件只供学习交流使用务作为商业用途交流群号286271084(已满) 2群649992274(已满)请加3群 群号632501142 - 本软件只供学习交流使用务作为商业用途交流群号286271084(已满) 2群649992274(已满)请加3群(未满) 群号632501142、4群(未满) 群号606340519
- 请不要重复加群,一个群就可以了,把机会留给更多人
- **进群先看公告!!!进群先看公告!!!进群先看公告!!! 重要的事情说三遍** - **进群先看公告!!!进群先看公告!!!进群先看公告!!! 重要的事情说三遍**
- 能为你抢到一张回家的票,是我最大的心愿 - 能为你抢到一张回家的票,是我最大的心愿

50
UnitTest/TestAll.py Normal file
View File

@ -0,0 +1,50 @@
# coding=utf-8
import unittest
from collections import OrderedDict
import requests
from agency.agency_tools import proxy
from config.emailConf import sendEmail
def _set_header_default():
header_dict = OrderedDict()
header_dict["Accept"] = "*/*"
header_dict["Accept-Encoding"] = "gzip, deflate"
header_dict["X-Requested-With"] = "superagent"
header_dict[
"User-Agent"] = "Mozilla/5.0 (iPhone; CPU iPhone OS 11_0 like Mac OS X) AppleWebKit/604.1.38 (KHTML, like Gecko) Version/11.0 Mobile/15A372 Safari/604.1"
header_dict["Content-Type"] = "application/x-www-form-urlencoded; charset=UTF-8"
class testAll(unittest.TestCase):
def testProxy(self):
"""
测试代理是否可用
:return:
"""
_proxy = proxy()
proxie = _proxy.setProxy()
url = "http://httpbin.org/ip"
rsp = requests.get(url, proxies=proxie, timeout=5, headers=_set_header_default()).content
print(u"当前代理ip地址为: {}".format(rsp))
def testEmail(self):
"""
实测邮箱是否可用
:return:
"""
sendEmail("订票小助手测试一下")
# def testConfig(self):
# """
# 测试config是否配置正确
# :return:
# """
if __name__ == '__main__':
unittest.main()

0
UnitTest/__init__.py Normal file
View File

View File

@ -133,3 +133,4 @@
- ticket_config 配置文件增加order_model字段下单模式 - ticket_config 配置文件增加order_model字段下单模式
- mac和linux服务器自动对点 - mac和linux服务器自动对点
- 增加预售踩点查询下单经测试误差在0.004s - 增加预售踩点查询下单经测试误差在0.004s

View File

@ -1,4 +1,6 @@
# encoding=utf8 # encoding=utf8
import os
import random
import socket import socket
import time import time
@ -41,7 +43,8 @@ class proxy:
:return: :return:
""" """
socket.setdefaulttimeout(1) socket.setdefaulttimeout(1)
f = open("./proxy_list", "w") path = os.path.join(os.path.dirname(__file__), './proxy_list')
f = open(path, "w")
head = { head = {
'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/50.0.2661.102 Safari/537.36', 'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/50.0.2661.102 Safari/537.36',
'Connection': 'keep-alive'} 'Connection': 'keep-alive'}
@ -55,7 +58,7 @@ class proxy:
write_proxy = proxy + "\n" write_proxy = proxy + "\n"
f.write(write_proxy) f.write(write_proxy)
proxy_num += 1 proxy_num += 1
except Exception, e: except Exception:
print ("代理链接超时去除此IP{0}".format(proxy)) print ("代理链接超时去除此IP{0}".format(proxy))
continue continue
print("总共可使用ip量为{}".format(proxy_num)) print("总共可使用ip量为{}".format(proxy_num))
@ -65,7 +68,8 @@ class proxy:
读取该可用ip文件 读取该可用ip文件
:return: 可用ip文件list :return: 可用ip文件list
""" """
f = open("./proxy_list", "r") path = os.path.join(os.path.dirname(__file__), './proxy_list')
with open(path, "r") as f:
lins = f.readlines() lins = f.readlines()
for i in lins: for i in lins:
p = i.strip("\n") p = i.strip("\n")
@ -73,10 +77,24 @@ class proxy:
return self.proxy_filter_list return self.proxy_filter_list
def main(self): def main(self):
self.get_proxy() # self.get_proxy()
self.filter_proxy() self.filter_proxy()
def setProxy(self):
"""
开启此功能的时候请确保代理ip是否可用
查询的时候设置代理ip,ip设置格式是ip地址+端口推荐可用的ip代理池https://github.com/jhao104/proxy_pool
:return:
"""
ip = self.get_filter_proxy()
setIp = ip[random.randint(0, len(ip) - 1)]
proxie = {
'http': 'http://{}'.format(setIp),
'https': 'http://{}'.format(setIp),
}
return proxie
if __name__ == "__main__": if __name__ == "__main__":
a = proxy() a = proxy()
a.main() print(a.get_filter_proxy())

1
agency/proxy_list Normal file
View File

@ -0,0 +1 @@
119.101.114.196:9999

View File

@ -14,8 +14,8 @@ class ticket(object):
TICKET_BLACK_LIST_TIME = 5 # 加入小黑屋的等待时间默认5 min TICKET_BLACK_LIST_TIME = 5 # 加入小黑屋的等待时间默认5 min
DTO_NOT_FOUND = u"未查找到常用联系人" DTO_NOT_FOUND = u"未查找到常用联系人, 请查证后添加!!"
DTO_NOT_IN_LIST = u"联系人不在列表中,请查证后添加" DTO_NOT_IN_LIST = u"联系人不在列表中,请查证后添加!!"
QUEUE_TICKET_SHORT = u"当前余票数小于乘车人数,放弃订票" QUEUE_TICKET_SHORT = u"当前余票数小于乘车人数,放弃订票"
QUEUE_TICKET_SUCCESS = u"排队成功, 当前余票还剩余: {0}" QUEUE_TICKET_SUCCESS = u"排队成功, 当前余票还剩余: {0}"

View File

@ -36,7 +36,7 @@ def sendEmail(msg):
smtp.quit() smtp.quit()
print(u"邮件已通知, 请查收") print(u"邮件已通知, 请查收")
except Exception as e: except Exception as e:
print(u"邮件配置有误", e) print(u"邮件配置有误{}".format(e))
else: else:
pass pass

View File

@ -30,24 +30,6 @@ set:
# - "G1353" # - "G1353"
# - "G1329" # - "G1329"
station_trains: station_trains:
# - "G6114"
# - "G1311"
# - "G6201"
# - "G821"
# - "G1019"
# - "G6587"
# - "G2905"
# - "G6345"
# - "G6033"
# - "G75"
# - "G6025"
# - "G6341"
# - "G1021"
# - "G99"
# - "G6011"
# - "D2972"
# - "D1874"
# - "D2834"
- "G6172" - "G6172"
- "G6186" - "G6186"
- "G6154" - "G6154"
@ -76,14 +58,14 @@ set:
# - "梁敏" # - "梁敏"
# 12306登录账号(list) # 12306登录账号(list)
12306account: 12306account:
- user: "931128603@qq.com" - user: "13828728396"
- pwd: "" - pwd: ""
# 加入小黑屋时间,此功能为了防止僵尸票导致一直下单不成功错过正常的票 # 加入小黑屋时间默认为5分钟,此功能为了防止僵尸票导致一直下单不成功错过正常的票
ticket_black_list_time: 5 ticket_black_list_time: 5
# 自动打码 # 自动打码
is_auto_code: False is_auto_code: True
# 打码平台, 2 为若快平台(目前只支持若快平台打码,打码兔已经关闭), 若快注册地址http://www.ruokuai.com/client/index?6726 # 打码平台, 2 为若快平台(目前只支持若快平台打码,打码兔已经关闭), 若快注册地址http://www.ruokuai.com/client/index?6726
auto_code_type: 2 auto_code_type: 2
@ -127,5 +109,12 @@ order_model: 2
# 预售放票时间, 如果是捡漏模式,可以忽略此操作 # 预售放票时间, 如果是捡漏模式,可以忽略此操作
open_time: '13:00:00' open_time: '13:00:00'
# 是否开启代理, 0代表关闭 1表示开始
# 开启此功能的时候请确保代理ip是否可用在测试放里面经过充分的测试再开启此功能不然可能会耽误你购票的宝贵时间
# 使用方法:
# 1、在agency/proxy_list列表下填入代理ip
# 2、测试UnitTest/TestAll/testProxy 测试代理是否可以用
# 3、开启代理ip
is_proxy: 0

View File

@ -47,11 +47,11 @@ class select:
self.from_station, self.to_station, self.station_dates, self._station_seat, self.is_more_ticket, \ self.from_station, self.to_station, self.station_dates, self._station_seat, self.is_more_ticket, \
self.ticke_peoples, self.station_trains, self.ticket_black_list_time, \ self.ticke_peoples, self.station_trains, self.ticket_black_list_time, \
self.order_type, self.is_by_time, self.train_types, self.departure_time, \ self.order_type, self.is_by_time, self.train_types, self.departure_time, \
self.arrival_time, self.take_time, self.order_model, self.open_time = self.get_ticket_info() self.arrival_time, self.take_time, self.order_model, self.open_time, self.is_proxy = self.get_ticket_info()
self.is_auto_code = _get_yaml()["is_auto_code"] self.is_auto_code = _get_yaml()["is_auto_code"]
self.auto_code_type = _get_yaml()["auto_code_type"] self.auto_code_type = _get_yaml()["auto_code_type"]
self.is_cdn = _get_yaml()["is_cdn"] self.is_cdn = _get_yaml()["is_cdn"]
self.httpClint = HTTPClient() self.httpClint = HTTPClient(self.is_proxy)
self.urls = urlConf.urls self.urls = urlConf.urls
self.login = GoLogin(self, self.is_auto_code, self.auto_code_type) self.login = GoLogin(self, self.is_auto_code, self.auto_code_type)
self.cdn_list = [] self.cdn_list = []
@ -88,8 +88,12 @@ class select:
order_model = ticket_info_config["order_model"] order_model = ticket_info_config["order_model"]
open_time = ticket_info_config["open_time"] open_time = ticket_info_config["open_time"]
# 代理模式
is_proxy = ticket_info_config["is_proxy"]
print(u"*" * 50) print(u"*" * 50)
print(u"12306刷票小助手最后更新于2019.01.07请勿作为商业用途交流群号286271084(已满) 2群649992274(已满)请加3群群号632501142") print(u"检查当前python版本为{}目前版本只支持2.7.10-2.7.15".format(sys.version.split(" ")[0]))
print(u"12306刷票小助手最后更新于2019.01.08请勿作为商业用途交流群号286271084(已满) 2群649992274(已满)请加3群(未满) 群号632501142、4群(未满) 群号606340519")
if is_by_time: if is_by_time:
method_notie = u"购票方式:根据时间区间购票\n可接受最早出发时间:{0}\n可接受最晚抵达时间:{1}\n可接受最长旅途时间:{2}\n可接受列车类型:{3}\n" \ method_notie = u"购票方式:根据时间区间购票\n可接受最早出发时间:{0}\n可接受最晚抵达时间:{1}\n可接受最长旅途时间:{2}\n可接受列车类型:{3}\n" \
.format(minutes_to_time(departure_time), minutes_to_time(arrival_time), minutes_to_time(take_time), .format(minutes_to_time(departure_time), minutes_to_time(arrival_time), minutes_to_time(take_time),
@ -114,7 +118,7 @@ class select:
print (u"*" * 50) print (u"*" * 50)
return from_station, to_station, station_dates, set_type, is_more_ticket, ticke_peoples, station_trains, \ return from_station, to_station, station_dates, set_type, is_more_ticket, ticke_peoples, station_trains, \
ticket_black_list_time, order_type, is_by_time, train_types, departure_time, arrival_time, take_time, \ ticket_black_list_time, order_type, is_by_time, train_types, departure_time, arrival_time, take_time, \
order_model, open_time order_model, open_time, is_proxy
def station_table(self, from_station, to_station): def station_table(self, from_station, to_station):
""" """
@ -146,7 +150,7 @@ class select:
def cdn_req(self, cdn): def cdn_req(self, cdn):
for i in range(len(cdn) - 1): for i in range(len(cdn) - 1):
http = HTTPClient() http = HTTPClient(0)
urls = self.urls["loginInitCdn"] urls = self.urls["loginInitCdn"]
http._cdn = cdn[i].replace("\n", "") http._cdn = cdn[i].replace("\n", "")
start_time = datetime.datetime.now() start_time = datetime.datetime.now()

View File

@ -22,7 +22,7 @@ def getPassCodeNewOrderAndLogin(session, imgType):
result = session.httpClint.send(codeImgUrl) result = session.httpClint.send(codeImgUrl)
try: try:
if isinstance(result, dict): if isinstance(result, dict):
print(u"下载验证码失败, 请手动检查是否ip被封或者重试请求地址https://kyfw.12306.cn/{}".format(codeImgUrl.get("req_url"))) print(u"下载验证码失败, 请手动检查是否ip被封或者重试请求地址https://kyfw.12306.cn{}".format(codeImgUrl.get("req_url")))
return False return False
else: else:
print(u"下载验证码成功") print(u"下载验证码成功")

View File

@ -38,6 +38,7 @@ class getPassengerDTOs:
elif getPassengerDTOsResult.get('messages', False): elif getPassengerDTOsResult.get('messages', False):
print(getPassengerDTOsResult.get('messages', False)) print(getPassengerDTOsResult.get('messages', False))
else: else:
print(getPassengerDTOsResult)
raise PassengerUserException(ticket.DTO_NOT_FOUND) raise PassengerUserException(ticket.DTO_NOT_FOUND)
def getPassengerTicketStr(self, set_type): def getPassengerTicketStr(self, set_type):

View File

@ -53,6 +53,7 @@ def codexy(Ofset=None, is_raw_input=True):
""") """)
print(u"验证码分为8个对应上面数字例如第一和第二张输入1, 2 如果开启cdn查询的话会冲掉提示直接鼠标点击命令行获取焦点输入即可不要输入空格") print(u"验证码分为8个对应上面数字例如第一和第二张输入1, 2 如果开启cdn查询的话会冲掉提示直接鼠标点击命令行获取焦点输入即可不要输入空格")
print(u"如果是linux无图形界面请使用自动打码is_auto_code: True") print(u"如果是linux无图形界面请使用自动打码is_auto_code: True")
print(u"如果没有弹出验证码请手动双击根目录下的tkcode.png文件")
Ofset = raw_input(u"输入对应的验证码: ") Ofset = raw_input(u"输入对应的验证码: ")
Ofset = Ofset.replace("", ",") Ofset = Ofset.replace("", ",")
select = Ofset.split(',') select = Ofset.split(',')

View File

@ -21,7 +21,7 @@ class query:
def __init__(self, session, from_station, to_station, from_station_h, to_station_h, _station_seat, station_trains, def __init__(self, session, from_station, to_station, from_station_h, to_station_h, _station_seat, station_trains,
ticke_peoples_num, station_dates=None, ): ticke_peoples_num, station_dates=None, ):
self.session = session self.session = session
self.httpClint = HTTPClient() self.httpClint = HTTPClient(session.is_proxy)
self.urls = urlConf.urls self.urls = urlConf.urls
self.from_station = from_station self.from_station = from_station
self.to_station = to_station self.to_station = to_station

View File

@ -4,6 +4,8 @@ import socket
from collections import OrderedDict from collections import OrderedDict
from time import sleep from time import sleep
import requests import requests
from agency.agency_tools import proxy
from config import logger from config import logger
@ -19,7 +21,7 @@ def _set_header_default():
class HTTPClient(object): class HTTPClient(object):
def __init__(self): def __init__(self, is_proxy):
""" """
:param method: :param method:
:param headers: Must be a dict. Such as headers={'Content_Type':'text/html'} :param headers: Must be a dict. Such as headers={'Content_Type':'text/html'}
@ -27,6 +29,10 @@ class HTTPClient(object):
self.initS() self.initS()
self._cdn = None self._cdn = None
self._proxies = None self._proxies = None
if is_proxy is 1:
self.proxy = proxy()
self._proxies = self.proxy.setProxy()
# print(u"设置当前代理ip为 {}, 请注意代理ip是否可用请注意代理ip是否可用请注意代理ip是否可用".format(self._proxies))
def initS(self): def initS(self):
self._s = requests.Session() self._s = requests.Session()
@ -134,6 +140,7 @@ class HTTPClient(object):
pass pass
response = self._s.request(method=method, response = self._s.request(method=method,
timeout=2, timeout=2,
proxies=self._proxies,
url="https://" + url_host + req_url, url="https://" + url_host + req_url,
data=data, data=data,
allow_redirects=allow_redirects, allow_redirects=allow_redirects,