前言
本文主要介绍 Py12306 抢票软件的手动安装和 Docker 安装过程,适用于 Centos/Debian/Ubuntu,目前主流开源的 12306 抢票软件有:testerSunshine/12306、pjialin/py12306。
Py12306 抢票功能介绍
- 多日期查询余票
- 自动打码下单
- 用户状态恢复
- 电话语音通知
- 多账号、多任务、多线程支持
- 单个任务多站点查询
- 分布式运行
- Docker 支持
- 动态修改配置文件
- 邮件通知
- Web 管理页面
- 微信消息通知
代理池支持 (pyproxy-async)
Python3.6 安装
Py12306 需要运行在 Python 3.6 以上版本。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34
| # apt install -y python3-pip
# apt-get update -y # apt-get install -y build-essential tk-dev libncurses5-dev libncursesw5-dev libreadline6-dev libdb5.3-dev libgdbm-dev libsqlite3-dev libssl-dev libbz2-dev libexpat1-dev liblzma-dev zlib1g-dev
# yum groupinstall -y "Development tools" # yum install -y sqlite-devel ncurses-devel ncurses-libs zlib-devel mysql-devel openssl-devel ncurses-devel sqlite-devel readline-devel tk-devel gdbm-devel db4-devel libpcap-devel xz-devel openssl-devel bzip2-devel expat-devel
# wget https://www.python.org/ftp/python/3.6.5/Python-3.6.5.tgz
# tar -xvf Python-3.6.5.tgz
# cd Python-3.6.5
# make # make install
# ln -s /usr/local/python3.6.5/bin/pip3.6 /usr/bin/pip3.6 # ln -s /usr/local/python3.6.5/bin/python3.6 /usr/bin/python3.6
# rm -rf Python-3.6.5 # rm -f Python-3.6.5.tgz
|
手动安装 Py12306
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33
| # git clone https://github.com/pjialin/py12306
# cd py12306
# pip3.6 install -r requirements.txt
# cp env.py.example env.py
# vim env.py
# python3.6 main.py -t
# python3.6 main.py -t -n
# python3.6 main.py
# nohup python3.6 main.py &
# tail -f runtime/12306.log
# firewall-cmd --zone=public --add-port=8008/tcp --permanent # firewall-cmd --reload
|
Docker 安装 Py12306
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
| # mkdir py12306
# cd py12306
# mkdir data
# docker run --rm pjialin/py12306 cat /config/env.py > env.py
# curl https://github.com/pjialin/py12306/blob/master/env.docker.py.example -o env.py
# vim env.py
# docker run -d --name py12306 -p 8008:8008 -v $(pwd):/config -v data:/data pjialin/py12306
# tail -f 12306.log
# firewall-cmd --zone=public --add-port=8008/tcp --permanent # firewall-cmd --reload
|
Docker-Compose 中使用 Py12306
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49
| # mkdir /usr/local/py12306
# cd /usr/local/py12306
# docker run --rm pjialin/py12306 cat /config/env.py > env.py
# curl https://github.com/pjialin/py12306/blob/master/env.docker.py.example -o env.py
# vim env.py
# touch docker-compose.yml
# vim docker-compose.yml version: "3.5" services: redis: image: pjialin/py12306 container_name: py12306 restart: always privileged: false ports: - 8008:8008 volumes: - '/usr/local/py12306:/config' - '/usr/local/py12306/env.py:/config/env.py' - '/usr/local/py12306/runtime:/code/runtime'
# docker-compose up -d
# tree /usr/local/py12306 ├── docker-compose.yml ├── 12306.log ├── env.py ├── query │ └── status.json ├── runtime └── user └── xxxxxxx.cookie
|
关于 IP 被屏蔽
目前查询和登录操作是分开的,查询是不依赖用户是否登录。12306 现在封服务器(阿里云和腾讯云)IP 比较严重,尽量不要在服务器环境下运行。关于分布式集群和代理池的支持,有兴趣的可以访问 Github 进一步学习。