Skip to content

Commit

Permalink
增加代理ip口子
Browse files Browse the repository at this point in the history
  • Loading branch information
testerSunshine committed Jan 8, 2019
1 parent 8e97d1d commit ad80669
Show file tree
Hide file tree
Showing 15 changed files with 120 additions and 47 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,8 @@
![image](https://github.com/testerSunshine/12306/blob/master/uml/uml.png)

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

Expand Down
50 changes: 50 additions & 0 deletions UnitTest/TestAll.py
Original file line number Diff line number Diff line change
@@ -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()
Empty file added UnitTest/__init__.py
Empty file.
1 change: 1 addition & 0 deletions Update.md
Original file line number Diff line number Diff line change
Expand Up @@ -133,3 +133,4 @@
- ticket_config 配置文件增加order_model字段(下单模式)
- mac和linux服务器自动对点
- 增加预售踩点查询下单,经测试,误差在0.004s
44 changes: 31 additions & 13 deletions agency/agency_tools.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
#encoding=utf8
# encoding=utf8
import os
import random
import socket
import time

Expand All @@ -22,7 +24,7 @@ def get_proxy(self):

for i in range(1, 5):
time.sleep(1)
url = 'http://www.xicidaili.com/nn/'+str(i)
url = 'http://www.xicidaili.com/nn/' + str(i)
res = requests.get(url=url, headers=header).content

soup = BeautifulSoup(res, "html.parser")
Expand All @@ -41,7 +43,8 @@ def filter_proxy(self):
:return:
"""
socket.setdefaulttimeout(1)
f = open("./proxy_list", "w")
path = os.path.join(os.path.dirname(__file__), './proxy_list')
f = open(path, "w")
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',
'Connection': 'keep-alive'}
Expand All @@ -52,10 +55,10 @@ def filter_proxy(self):
try:
req = requests.get(url, proxies=proxy_temp, timeout=2, headers=head).content
print(req)
write_proxy = proxy+"\n"
write_proxy = proxy + "\n"
f.write(write_proxy)
proxy_num += 1
except Exception, e:
except Exception:
print ("代理链接超时,去除此IP:{0}".format(proxy))
continue
print("总共可使用ip量为{}个".format(proxy_num))
Expand All @@ -65,18 +68,33 @@ def get_filter_proxy(self):
读取该可用ip文件
:return: 可用ip文件list
"""
f = open("./proxy_list", "r")
lins = f.readlines()
for i in lins:
p = i.strip("\n")
self.proxy_filter_list.append(p)
return self.proxy_filter_list
path = os.path.join(os.path.dirname(__file__), './proxy_list')
with open(path, "r") as f:
lins = f.readlines()
for i in lins:
p = i.strip("\n")
self.proxy_filter_list.append(p)
return self.proxy_filter_list

def main(self):
self.get_proxy()
# self.get_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__":
a = proxy()
a.main()
print(a.get_filter_proxy())
1 change: 1 addition & 0 deletions agency/proxy_list
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
119.101.114.196:9999
4 changes: 2 additions & 2 deletions config/TicketEnmu.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ class ticket(object):

TICKET_BLACK_LIST_TIME = 5 # 加入小黑屋的等待时间,默认5 min

DTO_NOT_FOUND = u"未查找到常用联系人"
DTO_NOT_IN_LIST = u"联系人不在列表中,请查证后添加"
DTO_NOT_FOUND = u"未查找到常用联系人, 请查证后添加!!"
DTO_NOT_IN_LIST = u"联系人不在列表中,请查证后添加!!"

QUEUE_TICKET_SHORT = u"当前余票数小于乘车人数,放弃订票"
QUEUE_TICKET_SUCCESS = u"排队成功, 当前余票还剩余: {0}张"
Expand Down
2 changes: 1 addition & 1 deletion config/emailConf.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ def sendEmail(msg):
smtp.quit()
print(u"邮件已通知, 请查收")
except Exception as e:
print(u"邮件配置有误", e)
print(u"邮件配置有误{}".format(e))
else:
pass

Expand Down
31 changes: 10 additions & 21 deletions config/ticket_config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -30,24 +30,6 @@ set:
# - "G1353"
# - "G1329"
station_trains:
# - "G6114"
# - "G1311"
# - "G6201"
# - "G821"
# - "G1019"
# - "G6587"
# - "G2905"
# - "G6345"
# - "G6033"
# - "G75"
# - "G6025"
# - "G6341"
# - "G1021"
# - "G99"
# - "G6011"
# - "D2972"
# - "D1874"
# - "D2834"
- "G6172"
- "G6186"
- "G6154"
Expand Down Expand Up @@ -76,14 +58,14 @@ set:
# - "梁敏"
# 12306登录账号(list)
12306account:
- user: "[email protected]"
- user: "13828728396"
- pwd: ""

# 加入小黑屋时间,此功能为了防止僵尸票导致一直下单不成功错过正常的票
# 加入小黑屋时间默认为5分钟,此功能为了防止僵尸票导致一直下单不成功错过正常的票
ticket_black_list_time: 5

# 自动打码
is_auto_code: False
is_auto_code: True

# 打码平台, 2 为若快平台(目前只支持若快平台打码,打码兔已经关闭), 若快注册地址:http://www.ruokuai.com/client/index?6726
auto_code_type: 2
Expand Down Expand Up @@ -127,5 +109,12 @@ order_model: 2
# 预售放票时间, 如果是捡漏模式,可以忽略此操作
open_time: '13:00:00'

# 是否开启代理, 0代表关闭, 1表示开始
# 开启此功能的时候请确保代理ip是否可用,在测试放里面经过充分的测试,再开启此功能,不然可能会耽误你购票的宝贵时间
# 使用方法:
# 1、在agency/proxy_list列表下填入代理ip
# 2、测试UnitTest/TestAll/testProxy 测试代理是否可以用
# 3、开启代理ip
is_proxy: 0


16 changes: 10 additions & 6 deletions init/select_ticket_info.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,11 +47,11 @@ def __init__(self):
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.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.auto_code_type = _get_yaml()["auto_code_type"]
self.is_cdn = _get_yaml()["is_cdn"]
self.httpClint = HTTPClient()
self.httpClint = HTTPClient(self.is_proxy)
self.urls = urlConf.urls
self.login = GoLogin(self, self.is_auto_code, self.auto_code_type)
self.cdn_list = []
Expand Down Expand Up @@ -88,16 +88,20 @@ def get_ticket_info(self):
order_model = ticket_info_config["order_model"]
open_time = ticket_info_config["open_time"]

# 代理模式
is_proxy = ticket_info_config["is_proxy"]

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:
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),
" , ".join(train_types))
else:
method_notie = u"购票方式:根据候选车次购买\n候选购买车次:{0}".format(",".join(station_trains))
print (u"当前配置:\n出发站:{0}\n到达站:{1}\n乘车日期:{2}\n坐席:{3}\n是否有票优先提交:{4}\n乘车人:{5}\n" \
u"刷新间隔: 随机(1-3S)\n{6}\n僵尸票关小黑屋时长: {7}\n下单接口: {8}\n下单模式: {9}\n预售踩点时间:{10} ".format \
u"刷新间隔: 随机(1-3S)\n{6}\n僵尸票关小黑屋时长: {7}\n下单接口: {8}\n下单模式: {9}\n预售踩点时间:{10} ".format \
(
from_station,
to_station,
Expand All @@ -114,7 +118,7 @@ def get_ticket_info(self):
print (u"*" * 50)
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, \
order_model, open_time
order_model, open_time, is_proxy

def station_table(self, from_station, to_station):
"""
Expand Down Expand Up @@ -146,7 +150,7 @@ def call_login(self, auth=False):

def cdn_req(self, cdn):
for i in range(len(cdn) - 1):
http = HTTPClient()
http = HTTPClient(0)
urls = self.urls["loginInitCdn"]
http._cdn = cdn[i].replace("\n", "")
start_time = datetime.datetime.now()
Expand Down
2 changes: 1 addition & 1 deletion inter/GetPassCodeNewOrderAndLogin.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ def getPassCodeNewOrderAndLogin(session, imgType):
result = session.httpClint.send(codeImgUrl)
try:
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
else:
print(u"下载验证码成功")
Expand Down
1 change: 1 addition & 0 deletions inter/GetPassengerDTOs.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ def sendGetPassengerDTOs(self):
elif getPassengerDTOsResult.get('messages', False):
print(getPassengerDTOsResult.get('messages', False))
else:
print(getPassengerDTOsResult)
raise PassengerUserException(ticket.DTO_NOT_FOUND)

def getPassengerTicketStr(self, set_type):
Expand Down
1 change: 1 addition & 0 deletions inter/GetRandCode.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ def codexy(Ofset=None, is_raw_input=True):
""")
print(u"验证码分为8个,对应上面数字,例如第一和第二张,输入1, 2 如果开启cdn查询的话,会冲掉提示,直接鼠标点击命令行获取焦点,输入即可,不要输入空格")
print(u"如果是linux无图形界面,请使用自动打码,is_auto_code: True")
print(u"如果没有弹出验证码,请手动双击根目录下的tkcode.png文件")
Ofset = raw_input(u"输入对应的验证码: ")
Ofset = Ofset.replace(",", ",")
select = Ofset.split(',')
Expand Down
2 changes: 1 addition & 1 deletion inter/Query.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ class query:
def __init__(self, session, from_station, to_station, from_station_h, to_station_h, _station_seat, station_trains,
ticke_peoples_num, station_dates=None, ):
self.session = session
self.httpClint = HTTPClient()
self.httpClint = HTTPClient(session.is_proxy)
self.urls = urlConf.urls
self.from_station = from_station
self.to_station = to_station
Expand Down
9 changes: 8 additions & 1 deletion myUrllib/httpUtils.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
from collections import OrderedDict
from time import sleep
import requests

from agency.agency_tools import proxy
from config import logger


Expand All @@ -19,14 +21,18 @@ def _set_header_default():

class HTTPClient(object):

def __init__(self):
def __init__(self, is_proxy):
"""
:param method:
:param headers: Must be a dict. Such as headers={'Content_Type':'text/html'}
"""
self.initS()
self._cdn = 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):
self._s = requests.Session()
Expand Down Expand Up @@ -134,6 +140,7 @@ def send(self, urls, data=None, **kwargs):
pass
response = self._s.request(method=method,
timeout=2,
proxies=self._proxies,
url="https://" + url_host + req_url,
data=data,
allow_redirects=allow_redirects,
Expand Down

0 comments on commit ad80669

Please sign in to comment.