PHP生活

记录生活里的一分一秒

PHP生活

记录生活里的一分一秒

手撕包菜SSBC(DHT磁力链源码)

环境:
Vultr日本    2G内存  7 x64   有公网IP
DO    1G内存      Centos7 x64     有公网IP
Linode    1G内存      Centos7 x64     有公网IP
[Vultr日本1 G内存在索引时报错:FATAL: out of memory (unable to allocate 1278738432 bytes)],可能是Vultr的VPS没有swap的原因
经测试,采集到的英文资源居多。

Table of Contents

本文环境:

Linode 2048

24GB DISK1 CPU Core2TB XFER.015/hr to $10/mo

一.确认python版本,获取ssbc源代码,关闭防火墙

1.环境检测(Ssbc当前版本是基于django1.8.1开发,所需python环境为python2.7.5以上。)

1

2

3

4

5

6

[root@localhost ~]# python -V    //执行python -V即可获取当前版本

Python 2.7.5

[root@localhost ~]# systemctl stop firewalld.service  //关闭firewall防火墙

[root@localhost ~]# systemctl disable firewalld.service    //禁止firewall防火墙开机启动

[root@localhost ~]# systemctl stop iptables.service   //关闭iptables防火墙

[root@localhost ~]# systemctl disable iptables.service    //禁止iptables防火墙开机启动

2.获取ssbc安装包并解压

1

2

3

[root@localhost ~]# wget https://github.com/78/ssbc/archive/master.zip

[root@localhost ~]# yum -y install unzip

[root@localhost ~]# unzip master.zip

解压后你会发现在/root目录下有个文件夹ssbc-master

二.安装MariaDB,创建数据库

1.安装数据库及所需环境

1

2

3

4

5

[root@localhost ~]# yum -y install gcc gcc-c++ python-devel mariadb mariadb-devel mariadb-server

[root@localhost ~]# cd ssbc-master

[root@localhost ssbcmaster]# wget https://bootstrap.pypa.io/get-pip.py

[root@localhost ssbcmaster]# python get-pip.py

[root@localhost ssbcmaster]# pip install -r requirements.txt

2.创建ssbc数据库

1

2

3

4

5

[root@localhost ssbcmaster]# systemctl start mariadb.service    //启动数据库

[root@localhost ssbcmaster]# mysql -uroot -p

Enter password: (回车即可)

MariaDB [(none)]> create database ssbc default character set utf8;

MariaDB [(none)]> \q    //创建成功后退出

三.安装Sphinx

1

2

3

4

[root@localhost ssbcmaster]# yum -y install unixODBC unixODBC-devel postgresql-libs

[root@localhost ssbcmaster]# wget http://sphinxsearch.com/files/sphinx-2.2.9-1.rhel7.x86_64.rpm

[root@localhost ssbcmaster]# rpm -ivh sphinx-2.2.9-1.rhel7.x86_64.rpm

[root@localhost ssbcmaster]# systemctl restart sphinx.service

四. 建立文件夹,创建以下文件夹并赋予755权限

1

2

3

[root@localhost ssbcmaster]# mkdir  -p  /data/bt/index/db /data/bt/index/binlog  /tem/downloads

[root@localhost ssbcmaster]# chmod  755 -R /data

[root@localhost ssbcmaster]# chmod  755 -R /tem

五. 生成索引

1

2

3

4

[root@localhost ssbcmaster]# systemctl restart mariadb.service  //重新启动Mariadb

[root@localhost ssbcmaster]# systemctl enable mariadb.service  //设置mariadb开启自启动

[root@localhost ssbcmaster]# indexer -c sphinx.conf –all     //(all 前面是空格减号减号)

[root@localhost ssbcmaster]# searchd –config ./sphinx.conf   //(config前是空格减号减号)

确定没有报错,继续下一步

六.Web服务器设置并启动

1

2

3

[root@localhost ssbcmaster]# python manage.py makemigrations

[root@localhost ssbcmaster]# python manage.py migrate

[root@localhost ssbcmaster]# nohup python manage.py runserver 0.0.0.0:80 >/dev/zero &    //启动网站并在后台运行

按回车键继续,这里可以使用其他端口然后前端再使用nginx做反向代理。手撕包菜SSBC(DHT磁力链源码) 前端使用 Nginx反向代理 80端口 图文教程
浏览器输入http://IP,网站能打开

七. 开启爬虫(workers目录下)

1

2

3

[root@localhost ssbcmaster]# cd workers

[root@localhost workers]# python simdht_worker.py  #爬虫运行,等2分钟出现数据之后CTRL+C停止

[root@localhost workers]# nohup python simdht_worker.py > /dev/zero  #让爬虫在后台运行

按回车键继

1

2

[root@localhost workers]# python index_worker.py  #入库索引,等待10分钟出现数据后CTRL+C停止

[root@localhost workers]# nohup python index_worker.py >/dev/zero #让索引在后台运行

按回车键继

增加后台管理员

1

2

[root@localhost workers]#cd ..

[root@localhost ssbcmaster]# python manage.py createsuperuser

输入管理员用户名
输入管理员邮箱
输入管理员密码
确认密码,完成
管理员登录地址:http://IP/admin

测试效果:
搜索中文英文关键字均正常
1

2


去除搜索页 右下角广告

[root@localhost ssbc-master]# cd web/static/js
[root@localhost js]# vi ssbc.js   找到如下3行,在前面添加//进行注释,保存
//        document.write(‘<script src=”https://www.jiloc.com/wp-content/uploads/201608074015uMCYx.jpg”><\/script>’);
//            document.writeln(“<script language=\”JavaScript\” type=\”text/javascript\” src=\”http://js.6dad.com/js/xiaoxia.js\”></script>”);
//           document.writeln(“<script language=\”JavaScript\” type=\”text/javascript\” src=\”http://js.ta80.com/js/12115.js\”></script>”);

1.搜索中文报错

‘ascii’ codec can’t encode characters in position 42-43: ordinal not in range(128)
2
解决办法:
如果是centos7系统,修改/usr/lib64/python2.7/site.py
vi  /usr/lib64/python2.7/site.py
在import sys下添加2行:

1

2

reload(sys)

sys.setdefaultencoding('utf8')

2.爬虫运行时 可能会遇到如下问题:

Python and Django OperationalError (2006, ‘MySQL server has gone away’)
解决方法:
http://stackoverflow.com/questions/14163429/python-and-django-operationalerror-2006-mysql-server-has-gone-away
/etc/my.cnf 在最后一行的下面添加

1

2

3

wait_timeout=2880000

interactive_timeout = 2880000

max_allowed_packet = 512M

常见问题:

1.必须centos7吗?

非常建议使用centos7,centos6可能会有意想不到的错误

2.怎么查看入库的文件?

登录管理员后台,点击 Hashs

3.怎么查看每天入库了多少文件,以便清楚入库效率?

登录管理员后台,点击 Status reports

4.如何确认web服务器、采集、入库正在运行?

1

ps ef|grep python

结果里面有
python manage.py runserver 0.0.0.0:80
python simdht_worker.py
python index_worker.py
即表示正在运行。

5.如出现没有模块pygeoip,请使用命令

1

pip install pygeoip


 

进行安装

[root@li433-14 ssbc-master]# python workers/simdht_worker.py
Traceback (most recent call last):
File “workers/simdht_worker.py”, line 28, in <module>
import pygeoip
ImportError: No module named pygeoip

6.IOError: [Errno 2] No such file or directory: ‘GeoIP.dat’提示错误,解决办法:

从http://geolite.maxmind.com/download/geoip/database/GeoLiteCountry/GeoIP.dat.gz下载GeoIP包,上传到/root/ssbc-master文件夹即可。

[root@li433-14 ssbc-master]# python workers/simdht_worker.py
exceptions must be old-style classes or derived from BaseException, not NoneType
exceptions must be old-style classes or derived from BaseException, not NoneType
Traceback (most recent call last):
File “workers/simdht_worker.py”, line 39, in <module>
import simMetadata
File “/root/ssbc-master/workers/simMetadata.py”, line 11, in <module>
from simdht_worker import entropy
File “/root/ssbc-master/workers/simdht_worker.py”, line 41, in <module>
from metadata import save_metadata
File “/root/ssbc-master/workers/metadata.py”, line 14, in <module>
geoip = pygeoip.GeoIP(‘GeoIP.dat’)
File “/usr/lib/python2.7/site-packages/pygeoip/__init__.py”, line 75, in __call__
cls._instances[filename] = super(_GeoIPMetaclass, cls).__call__(*args, **kwargs)
File “/usr/lib/python2.7/site-packages/pygeoip/__init__.py”, line 118, in __init__
self._fp = codecs.open(filename, ‘rb’, ENCODING)
File “/usr/lib64/python2.7/codecs.py”, line 881, in open
file = __builtin__.open(filename, mode, buffering)
IOError: [Errno 2] No such file or directory: ‘GeoIP.dat’

7.”Can’t connect to MySQL server on ’12 7.0.0.1′ (111)”,无法连接到数据库。

在运行脚本之前需要修改2个文件,因为从git上下载下来的源码里写的mariadb的端口是9306,我们这里做一下修改改成3306,否则无法启动。workers/index_worker.py:24行,search/models.py:13行,把9306替换成3306。否则会出现无法连接数据库。

可能是因为sphinx的原因造成的,由于先前的版本是先运行脚本然后再运行sphinx,所以会报错,这里我们已经将过程进行对调。

[root@li433-14 ssbc-master]# python manage.py makemigrations
Traceback (most recent call last):
File “manage.py”, line 10, in <module>
execute_from_command_line(sys.argv)
File “/usr/lib/python2.7/site-packages/django/core/management/__init__.py”, li ne 338, in execute_from_command_line
utility.execute()
File “/usr/lib/python2.7/site-packages/django/core/management/__init__.py”, li ne 312, in execute
django.setup()
File “/usr/lib/python2.7/site-packages/django/__init__.py”, line 18, in setup
apps.populate(settings.INSTALLED_APPS)
File “/usr/lib/python2.7/site-packages/django/apps/registry.py”, line 108, in populate
app_config.import_models(all_models)
File “/usr/lib/python2.7/site-packages/django/apps/config.py”, line 198, in im port_models
self.models_module = import_module(models_module_name)
File “/usr/lib64/python2.7/importlib/__init__.py”, line 37, in import_module
__import__(name)
File “/root/ssbc-master/search/models.py”, line 13, in <module>
search_conn = mdb.connect(‘127.0.0.1’, ‘root’, ”, ”, port=9306, charset=’u tf8′, cursorclass=MySQLdb.cursors.DictCursor)
File “/usr/lib64/python2.7/site-packages/MySQLdb/__init__.py”, line 81, in Con nect
return Connection(*args, **kwargs)
File “/usr/lib64/python2.7/site-packages/MySQLdb/connections.py”, line 193, in __init__
super(Connection, self).__init__(*args, **kwargs2)
_mysql_exceptions.OperationalError: (2003, “Can’t connect to MySQL server on ’12 7.0.0.1′ (111)”)

[root@li433-14 ssbc-master]# grep -rn 9306 /root/ssbc-master    #查找一下文件夹中包含9306的文件
workers/index_worker.py:24:dst_conn = mdb.connect(DST_HOST, DST_USER, DST_PASS, ‘rt_main’, port=9306, charset=’utf8′)
search/models.py:13:search_conn = mdb.connect(‘127.0.0.1’, ‘root’, ”, ”, port 9306, charset=’utf8′, cursorclass=MySQLdb.cursors.DictCursor)

手撕包菜SSBC(DHT磁力链源码)

发表回复

滚动到顶部