java dht 爬虫_python开发的 dht网络爬虫
使用 libtorrent 的python绑定库实现一个dht网络爬虫,抓取dht网络中的磁力链接。
dht 网络简介
p2p网络
在P2P网络中,通过种子文件下载资源时,要知道资源在P2P网络中哪些计算机中,这些传输资源的计算机称作peer。在传统的P2P网络中,使用tracker服务器跟踪资源的peer。要下载资源,首先需要取得这些peer。
dht网络
tracker服务器面临一些版权和法律问题。于是出现了DHT,它把tracker上的资源peer信息分散到了整个网络中。dht网络是由分布 式节点构成,节点(node)是实现了DHT协议的p2p客户端。P2P客户端程序既是peer也是node。DHT网络有多种算法,常用的有 Kademlia。
dht网络下载
P2P客户端使用种子文件下载资源时,如果没有tracker服务器,它就向DHT网络查询资源的peer列表, 然后从peer下载资源。
Magnet是磁力链接
资源的标识在DHT网络中称为infohash,是一个通过sha1算法得到的20字节长的字符串。infohash是使用种子文件的文件描述信息 计算得到。磁力链接是把infohash编码成16进制字符串得到。P2P客户端使用磁力链接,下载资源的种子文件,然后根据种子文件下载资源。
Kademlia 算法
Kademlia是DHT网络的一种实现, 具体的算法参见:DHT协议
KRPC 协议
KRPC 是节点之间的交互协议,使用UDP来传送。
包括4种请求:ping,find_node,get_peer,announce_peer。其中get_peer和announce_peer是节点间查询资源的主要消息。
dht 爬虫原理
主要的思路就是伪装为p2p客户端,加入dht网络,收集dht网络中的get_peer和announce_peer消息,这些消息是其他node发送给伪装的p2p客户端的udp消息。
本文dht爬虫的实现
爬虫运行环境
linux 系统
python 2.7
libtorrent 库的python绑定
twisted 网络库
防火墙开启固定的udp和tcp端口
libtorrent 库的介绍
libtorrent库是p2p下载的客户端库,有丰富的接口,可以用来开发下载p2p网络上的资源。它有python的绑定库,本爬虫就是使用它的python库开发的。
在libtorrent中有几个概念需要解释下。 session 相当于p2p客户端,session开启一个tcp和一个udp端口,用来与其他p2p客户端交换数据。可以在一个进程内定义多个session,也就是多个p2p客户端,来加快收集速度。
alert是libtorrent中用来收集各种消息的队列,每个session都有一个自己的alert消息队列。KRPC协议的get_peer和announce_peer消息也是从这个队列中获取,就是用这两个消息收集磁力链接的。
主要实现代码
爬虫实现的主要代码比较简单
# 事件通知处理函数
def _handle_alerts(self, session, alerts):
while len(alerts):
alert = alerts.pop()
# 获取dht_announce_alert和dht_get_peer_alert消息
# 从这两消息收集磁力链接
if isinstance(alert, lt.add_torrent_alert):
alert.handle.set_upload_limit(self._torrent_upload_limit)
alert.handle.set_download_limit(self._torrent_download_limit)
elif isinstance(alert, lt.dht_announce_alert):
info_hash = alert.info_hash.to_string().encode('hex')
if info_hash in self._meta_list:
self._meta_list[info_hash] += 1
else:
self._meta_list[info_hash] = 1
self._current_meta_count += 1
elif isinstance(alert, lt.dht_get_peers_alert):
info_hash = alert.info_hash.to_string().encode('hex')
if info_hash in self._meta_list:
self._meta_list[info_hash] += 1
else:
self._infohash_queue_from_getpeers.append(info_hash)
self._meta_list[info_hash] = 1
self._current_meta_count += 1
def start_work(self):
'''主工作循环,检查消息,显示状态'''
# 清理屏幕
begin_time = time.time()
show_interval = self._delay_interval
while True:
for session in self._sessions:
session.post_torrent_updates()
# 从队列中获取信息
self._handle_alerts(session, session.pop_alerts())
time.sleep(self._sleep_time)
if show_interval > 0:
show_interval -= 1
continue
show_interval = self._delay_interval
# 统计信息显示
show_content = ['torrents:']
interval = time.time() - begin_time
show_content.append(' pid: %s' % os.getpid())
show_content.append(' time: %s' %
time.strftime('%Y-%m-%d %H:%M:%S'))
show_content.append(' run time: %s' % self._get_runtime(interval))
show_content.append(' start port: %d' % self._start_port)
show_content.append(' collect session num: %d' %
len(self._sessions))
show_content.append(' info hash nums from get peers: %d' %
len(self._infohash_queue_from_getpeers))
show_content.append(' torrent collection rate: %f /minute' %
(self._current_meta_count * 60 / interval))
show_content.append(' current torrent count: %d' %
self._current_meta_count)
show_content.append(' total torrent count: %d' %
len(self._meta_list))
show_content.append('\n')
# 存储运行状态到文件
try:
with open(self._stat_file, 'wb') as f:
f.write('\n'.join(show_content))
with open(self._result_file, 'wb') as f:
json.dump(self._meta_list, f)
except Exception as err:
pass
# 测试是否到达退出时间
if interval >= self._exit_time:
# stop
break
# 每天结束备份结果文件
self._backup_result()
# 销毁p2p客户端
for session in self._sessions:
torrents = session.get_torrents()
for torrent in torrents:
session.remove_torrent(torrent)
运行效率
在我的一台512M内存,单cpu机器上。爬虫刚开始运行稍慢,运行几分钟后收集速度稳定在 180个每分钟,1小时采集10000左右。
运行状态
run times: 12
torrents:
pid: 11480 time: 2014-08-18 22:45:01
run time: day: 0, hour: 0, minute: 12, second: 25
start port: 32900
collect session num: 20
info hash nums from get peers: 2222
torrent collection rate: 179.098480 /minute
current torrent count: 2224
total torrent count: 58037
爬虫完整代码
还包括一个基于twisted的监控进程,用来查看爬虫状态,在爬虫进程退出后重新启动。
java dht 爬虫_python开发的 dht网络爬虫相关推荐
- 【spider】爬虫学习路线-精通Scrapy网络爬虫
博客已经搬家到"捕获完成": https://www.v2python.com 随着大数据时代的到来,人们对数据资源的需求越来越多,而爬虫是一种很好的自动采集数据的手段. 那么,如 ...
- 【python爬虫 系列】1.理解网络爬虫
第一节:理解网络爬虫 1.1网络爬虫的定义 网络爬虫(又被称为网页蜘蛛,网络机器人)就是模拟浏览器发送网络请求,接收请求响应,一种按照一定的规则,自动地抓取互联网信息的程序.另外一些不常使用的名字还有 ...
- 网络爬虫python实例视频-Python网络爬虫实例教程 视频讲解版
章网络爬虫概述1 1.1认识网络爬虫1 1.1.1网络爬虫的含义1 1.1.2网络爬虫的主要类型2 1.1.3简单网络爬虫的架构3 1.1.4网络爬虫的应用场景3 1.2Python网络爬虫技术概况4 ...
- 什么是网络爬虫,我们为什么要学习网络爬虫?
一.什么是网络爬虫 网络爬虫又称网络蜘蛛.网络蚂蚁.网络机器人等,可以自动化浏览网络中的信息,当然浏览信息的时候需要按照我们制定的规则进行,这些规则我们称之为网络爬虫算法.使用Python可以很方 ...
- Python爬虫总结——Scrapy+Gerapy部署网络爬虫
Python爬虫总结--从基础爬虫到Scrapy+Gerapy部署网络爬虫 前言 一.常用pip模块介绍 1.NumPy库 2.Pandas库 3.Requests库 4.BeautifulSoup库 ...
- 爬虫基础(2)网络爬虫的实现原理与技术
文章目录 一. 爬虫技术实现原理 二. 发送请求 1. 请求行 2. 请求头 3. 空行 4. 请求体 三. 获取响应内容 1. 响应行 2. 响应头 3. 空行 4. 响应体 四. 解析网页内容 1 ...
- python网络爬虫技术-基于Python的网络爬虫技术综述
汪洋 姜新通 [摘 要]人类社会已经进入大数据时代,这正在改变着我们的工作和生活.随着互联网的兴起和发展,同时也产生了各种对人类有价值的数据.快速搜索数据和充分利用数据信息已成为一个巨大挑战.这样的需 ...
- 【python爬虫学习篇】初识网络爬虫以及了解Web前端
目录 1,初识爬虫 1.1,网络爬虫概述 1.2,爬虫的分类 1.3,网络爬虫的基本原理 1.4,搭建开发环境 2,了解web前端 2.1,HTTP基本原理 2.1.1HTTP协议 2.1.2,Web ...
- java 开发用到网络爬虫,抓取汽车之家网站全部数据经历
经历了两个礼拜的折腾,某某知名网站的数据终于到手了.犯罪没被发现这种心情感觉很爽. 说一下我的犯罪经历,之前公司总是抓取某某网站数据,可能是被发现了.某某网站改变了策略.通过各种技术终止了我们的行为, ...
- python3网络爬虫代码_《Python3网络爬虫开发实战代码》
<Python3网络爬虫开发实战代码>\appium\.git\COMMIT_EDITMSG, 7 , 2017-08-15 <Python3网络爬虫开发实战代码>\appiu ...
最新文章
- centos的ssh配置
- 巧用伪元素和伪类让我们的html结构更清晰合理
- [Unity3d]制作打包并载入AssetBundle
- Syntax error , insert “EnumBody” to complete EnumDeclaration
- P4语言编程快速开始 实践二
- php webshell开源,[github开源]webshell连接器--Jeshell
- pacemaker+corosync实现集群管理
- java如何编写年月_如何从Java中的日历对象构建天,月,年的列表?
- 通风与防排烟工程电子书_工厂防排烟系统与通风空调系统的兼用设计
- JAVA线程1 - 基本概念
- java异步文件读写文件,Java AsynchronousFileChannel和Future读取文件
- 少儿计算机基础知识,儿童计算机基本操作
- linux安装rz命令
- Vegas渲染的时候选什么格式?
- 台式计算机锁屏快捷键,台式机锁屏快捷键是什么
- 【中文乱码】深入分析 Java Web 中的中文编码问题
- 昆明站 · 参会指南 | 2018中国软件生态大会
- context:component-scan base-package=com.xxx.xxx/ 包扫描注解
- matlab粒子群加约束条件_matlab粒子群编程,等式约束如何加入
- 菜鸟要飞java_Retrofit2[](更多视频教程关注微信公众号【菜鸟要飞】)等
热门文章
- 微PE制作纯净U盘启动器及CGI安装镜像
- 中国大学MOOC 视频字幕获取及处理方法
- PDF Suite Pro(PDF全能工具箱电脑版)官方正式版V19.0.22 | 含ocr文字识别软件 | pdf全能工具箱怎么样?
- labelme标注的json文件转换成png格式
- 手机屏幕测试html,华为手机屏幕检测代码是什么
- 【MATLAB教程案例11~20总结】优化类算法matlab仿真经验和技巧总结
- 手动 将exe加入到系统启动服务、卸载服务的方法
- 用计算机画图软件画画教程,电脑画图软件有什么使用技巧,电脑画图软件教程...
- MTK 刷机工具操作说明(多路)
- 2021-05-11