下载工具 aria2 控制文件(.aria2)解析

news/2025/3/14 12:07:22/文章来源:https://www.cnblogs.com/brian-sun/p/18771838

.aria2c 定义

参考文档:https://aria2.document.top/zh/technical-notes.html#aria2

解析脚本

parse_aria2c.py

import os
import sys
import struct
from datetime import datetimedef Usage():print("Usage: python3 parse_aria2c.py <aria2c_file_path> <show_detail>")print("show_detail: True or False")print("Example: python3 parse_aria2c.py aria2c_file True")def read_bytes(file, offset, length):"""读取指定偏移和长度的字节"""file.seek(offset)return file.read(length)def format_size(bytes):"""将字节大小转换为适当的单位(KB, MB, GB等),支持负数。:param bytes: 原始字节大小,可以为负数:return: 字符串,格式化后的大小和单位"""# 定义单位和阈值units = ["B", "KB", "MB", "GB", "TB", "PB", "EB", "ZB", "YB"]# 记录负号sign = "-" if bytes < 0 else ""# 转为正数进行计算size = abs(bytes)index = 0# 每次除以1024,直到大小合适或达到最大单位while size >= 1024 and index < len(units) - 1:size /= 1024.0index += 1# 格式化输出,保留两位小数,并加上负号(如有)return f"{sign}{size:.2f} {units[index]}"def parse_file(file_path, show_detail=False):print(f"Start parsing file:{file_path}")file_stat = os.stat(file_path)print("------ .aria2 FILE STAT ------")print(f"Size: \t\t{format_size(file_stat.st_size)}")print(f"Modified Time: \t{datetime.fromtimestamp(file_stat.st_mtime)}")print(f"Access Time: \t{datetime.fromtimestamp(file_stat.st_atime)}")print(f"Creation Time: \t{datetime.fromtimestamp(file_stat.st_ctime)}")print()print("------ .aria2 FILE INFO ------")with open(file_path, "rb") as file:# VER (版本): 2 bytes# 从偏移量为 0 开始解析 VER (版本)ver = struct.unpack(">H", read_bytes(file, 0, 2))[0]print(f"Version: {ver}")# EXT (扩展): 4 bytes# 解析 EXT (扩展)ext = struct.unpack(">I", read_bytes(file, 2, 4))[0]print(f"Extension: {ext}")# INFO HASH LENGTH: 4 bytes# 解析 InfoHash Lengthinfo_hash_length = struct.unpack(">I", read_bytes(file, 6, 4))[0]print(f"InfoHash Length: {info_hash_length}")# INFO HASH: (INFO HASH LENGTH) bytes# 解析 InfoHashinfo_hash = read_bytes(file, 10, info_hash_length).hex()print(f"InfoHash: {info_hash}")current_offset = 10 + info_hash_length# PIECE LENGTH: 4 bytes# 解析 Piece Lengthpiece_length = struct.unpack(">I", read_bytes(file, current_offset, 4))[0]print(f"Piece Length: {piece_length} ({format_size(piece_length)})")current_offset += 4# TOTAL LENGTH: 8 bytes# 解析 Total Lengthtotal_length = struct.unpack(">Q", read_bytes(file, current_offset, 8))[0]print(f"Total Length: {total_length} ({format_size(total_length)})")current_offset += 8# UPLOAD LENGTH: 8 bytes# 解析 Upload Lengthupload_length = struct.unpack(">Q", read_bytes(file, current_offset, 8))[0]print(f"Upload Length: {upload_length} ({format_size(upload_length)})")current_offset += 8# BITFIELD LENGTH: 4 bytes# 解析 Bitfield Lengthbitfield_length = struct.unpack(">I", read_bytes(file, current_offset, 4))[0]print(f"Bitfield Length: {bitfield_length}")current_offset += 4# BITFIELD: (BITFIELD LENGTH) bytes# 解析 Bitfieldbitfield = read_bytes(file, current_offset, bitfield_length).hex()print(f"Bitfield: {bitfield}")current_offset += bitfield_length# NUM IN-FLIGHT PIECE: 4 bytes# 解析 Num In-Flight Piecenum_in_flight_piece = struct.unpack(">I", read_bytes(file, current_offset, 4))[0]print(f"Num In-Flight Piece: {num_in_flight_piece}")current_offset += 4# 以下4个字段重复 (NUM IN-FLIGHT PIECE) 次数。# INDEX: 4 bytes# LENGTH: 4 bytes# PIECE BITFIELD LENGTH: 4 bytes# PIECE BITFIELD: (PIECE BITFIELD LENGTH) bytes# 循环解析每个 Num In-Flight Piece 的字段for i in range(num_in_flight_piece):index = struct.unpack(">I", read_bytes(file, current_offset, 4))[0]length = struct.unpack(">I", read_bytes(file, current_offset + 4, 4))[0]piece_bitfield_length = struct.unpack(">I", read_bytes(file, current_offset + 8, 4))[0]current_offset += 12  # 跳过 Index (4 bytes), Length (4 bytes), Piece Bitfield Length (4 bytes)# 解析 Piece Bitfieldpiece_bitfield = read_bytes(file, current_offset, piece_bitfield_length).hex()current_offset += piece_bitfield_lengthif show_detail:print(f"In-Flight Piece {i}: Index: {index}, Length: {length} ({format_size(length)}), Piece Bitfield Length: {piece_bitfield_length}, Piece Bitfield: {piece_bitfield}")else:print(f"In-Flight Piece {i}: Index: {index}, Length: {length} ({format_size(length)})")if __name__ == '__main__':argv = sys.argvif len(argv) not in (2,3):Usage()sys.exit(1)show_detail=Falsefile_path = argv[1]if len(argv) == 3:show_detail_str = argv[2]if show_detail_str.lower() in ("true","false"):show_detail = True if show_detail_str.lower() == "true" else Falseelse:Usage()sys.exit(1)if not os.path.exists(file_path):print(f"File:{file_path} not exists")sys.exit(1)parse_file(file_path, show_detail)

运行效果:

#  python3 parse_aria2c.py tst.aria2
# 简化模式
------ .aria2 FILE STAT ------
Size:           3.05 KB
Modified Time:  2025-03-14 10:44:14.239716
Access Time:    2025-03-14 10:47:17.781647
Creation Time:  2025-03-14 10:44:14.239716------ .aria2 FILE INFO ------
Version: 1
Extension: 0
InfoHash Length: 0
InfoHash: 
Piece Length: 10485760 (10.00 MB)
Total Length: 12185573232 (11.35 GB)
Upload Length: 0 (0.00 B)
Bitfield Length: 146
Bitfield: ffffffffffffe000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
Num In-Flight Piece: 32
In-Flight Piece 0: Index: 51, Length: 10485760 (10.00 MB)
In-Flight Piece 1: Index: 52, Length: 10485760 (10.00 MB)
In-Flight Piece 2: Index: 53, Length: 10485760 (10.00 MB)
In-Flight Piece 3: Index: 54, Length: 10485760 (10.00 MB)
In-Flight Piece 4: Index: 55, Length: 10485760 (10.00 MB)
In-Flight Piece 5: Index: 56, Length: 10485760 (10.00 MB)
In-Flight Piece 6: Index: 57, Length: 10485760 (10.00 MB)
In-Flight Piece 7: Index: 58, Length: 10485760 (10.00 MB)
In-Flight Piece 8: Index: 59, Length: 10485760 (10.00 MB)
In-Flight Piece 9: Index: 60, Length: 10485760 (10.00 MB)
In-Flight Piece 10: Index: 61, Length: 10485760 (10.00 MB)
In-Flight Piece 11: Index: 62, Length: 10485760 (10.00 MB)
In-Flight Piece 12: Index: 63, Length: 10485760 (10.00 MB)
In-Flight Piece 13: Index: 64, Length: 10485760 (10.00 MB)
In-Flight Piece 14: Index: 65, Length: 10485760 (10.00 MB)
In-Flight Piece 15: Index: 66, Length: 10485760 (10.00 MB)
In-Flight Piece 16: Index: 67, Length: 10485760 (10.00 MB)
In-Flight Piece 17: Index: 68, Length: 10485760 (10.00 MB)
In-Flight Piece 18: Index: 69, Length: 10485760 (10.00 MB)
In-Flight Piece 19: Index: 70, Length: 10485760 (10.00 MB)
In-Flight Piece 20: Index: 71, Length: 10485760 (10.00 MB)
In-Flight Piece 21: Index: 72, Length: 10485760 (10.00 MB)
In-Flight Piece 22: Index: 73, Length: 10485760 (10.00 MB)
In-Flight Piece 23: Index: 74, Length: 10485760 (10.00 MB)
In-Flight Piece 24: Index: 75, Length: 10485760 (10.00 MB)
In-Flight Piece 25: Index: 76, Length: 10485760 (10.00 MB)
In-Flight Piece 26: Index: 77, Length: 10485760 (10.00 MB)
In-Flight Piece 27: Index: 78, Length: 10485760 (10.00 MB)
In-Flight Piece 28: Index: 79, Length: 10485760 (10.00 MB)
In-Flight Piece 29: Index: 80, Length: 10485760 (10.00 MB)
In-Flight Piece 30: Index: 81, Length: 10485760 (10.00 MB)
In-Flight Piece 31: Index: 82, Length: 10485760 (10.00 MB)#  python3 parse_aria2c.py tst.aria2 True
# 详细模式
------ .aria2 FILE STAT ------
Size:           3.05 KB
Modified Time:  2025-03-14 10:44:14.239716
Access Time:    2025-03-14 10:47:17.781647
Creation Time:  2025-03-14 10:44:14.239716------ .aria2 FILE INFO ------
Version: 1
Extension: 0
InfoHash Length: 0
InfoHash: 
Piece Length: 10485760 (10.00 MB)
Total Length: 12185573232 (11.35 GB)
Upload Length: 0 (0.00 B)
Bitfield Length: 146
Bitfield: ffffffffffffe000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
Num In-Flight Piece: 32
In-Flight Piece 0: Index: 51, Length: 10485760 (10.00 MB), Piece Bitfield Length: 80, Piece Bitfield: ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffe00000000000000000
In-Flight Piece 1: Index: 52, Length: 10485760 (10.00 MB), Piece Bitfield Length: 80, Piece Bitfield: fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff80000000000000000000000000000
In-Flight Piece 2: Index: 53, Length: 10485760 (10.00 MB), Piece Bitfield Length: 80, Piece Bitfield: fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff000000000000000000000000000
In-Flight Piece 3: Index: 54, Length: 10485760 (10.00 MB), Piece Bitfield Length: 80, Piece Bitfield: fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff00000000000000000000000000000
In-Flight Piece 4: Index: 55, Length: 10485760 (10.00 MB), Piece Bitfield Length: 80, Piece Bitfield: fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffe0000000000000000000000000000000000000000
In-Flight Piece 5: Index: 56, Length: 10485760 (10.00 MB), Piece Bitfield Length: 80, Piece Bitfield: ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffc00000000000000000000000000000000000000000000000000000000000
In-Flight Piece 6: Index: 57, Length: 10485760 (10.00 MB), Piece Bitfield Length: 80, Piece Bitfield: fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff000000000000000000000000000000000000000000000000000000000000000000000000000000000
In-Flight Piece 7: Index: 58, Length: 10485760 (10.00 MB), Piece Bitfield Length: 80, Piece Bitfield: fffffffffffffffffffffffffffffffffffffffffffffffffffffff000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
In-Flight Piece 8: Index: 59, Length: 10485760 (10.00 MB), Piece Bitfield Length: 80, Piece Bitfield: fffffffffffffffffffffffffffffffffffffffffffe00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
In-Flight Piece 9: Index: 60, Length: 10485760 (10.00 MB), Piece Bitfield Length: 80, Piece Bitfield: ffffffffffffffffffffffffffffff8000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
In-Flight Piece 10: Index: 61, Length: 10485760 (10.00 MB), Piece Bitfield Length: 80, Piece Bitfield: ffffffffffffffffffffffffffffffffffff8000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
In-Flight Piece 11: Index: 62, Length: 10485760 (10.00 MB), Piece Bitfield Length: 80, Piece Bitfield: ffffffffffffffffffffffffffff000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
In-Flight Piece 12: Index: 63, Length: 10485760 (10.00 MB), Piece Bitfield Length: 80, Piece Bitfield: fffffffffffffffc000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
In-Flight Piece 13: Index: 64, Length: 10485760 (10.00 MB), Piece Bitfield Length: 80, Piece Bitfield: ffffffffffffffffffffffffffff800000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
In-Flight Piece 14: Index: 65, Length: 10485760 (10.00 MB), Piece Bitfield Length: 80, Piece Bitfield: ffffffffffffffffffffffc00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
In-Flight Piece 15: Index: 66, Length: 10485760 (10.00 MB), Piece Bitfield Length: 80, Piece Bitfield: ffffffffffffffffffe000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
In-Flight Piece 16: Index: 67, Length: 10485760 (10.00 MB), Piece Bitfield Length: 80, Piece Bitfield: 0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
In-Flight Piece 17: Index: 68, Length: 10485760 (10.00 MB), Piece Bitfield Length: 80, Piece Bitfield: 0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
In-Flight Piece 18: Index: 69, Length: 10485760 (10.00 MB), Piece Bitfield Length: 80, Piece Bitfield: 0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
In-Flight Piece 19: Index: 70, Length: 10485760 (10.00 MB), Piece Bitfield Length: 80, Piece Bitfield: 0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
In-Flight Piece 20: Index: 71, Length: 10485760 (10.00 MB), Piece Bitfield Length: 80, Piece Bitfield: 0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
In-Flight Piece 21: Index: 72, Length: 10485760 (10.00 MB), Piece Bitfield Length: 80, Piece Bitfield: 0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
In-Flight Piece 22: Index: 73, Length: 10485760 (10.00 MB), Piece Bitfield Length: 80, Piece Bitfield: 0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
In-Flight Piece 23: Index: 74, Length: 10485760 (10.00 MB), Piece Bitfield Length: 80, Piece Bitfield: 0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
In-Flight Piece 24: Index: 75, Length: 10485760 (10.00 MB), Piece Bitfield Length: 80, Piece Bitfield: 0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
In-Flight Piece 25: Index: 76, Length: 10485760 (10.00 MB), Piece Bitfield Length: 80, Piece Bitfield: 0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
In-Flight Piece 26: Index: 77, Length: 10485760 (10.00 MB), Piece Bitfield Length: 80, Piece Bitfield: 0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
In-Flight Piece 27: Index: 78, Length: 10485760 (10.00 MB), Piece Bitfield Length: 80, Piece Bitfield: 0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
In-Flight Piece 28: Index: 79, Length: 10485760 (10.00 MB), Piece Bitfield Length: 80, Piece Bitfield: 0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
In-Flight Piece 29: Index: 80, Length: 10485760 (10.00 MB), Piece Bitfield Length: 80, Piece Bitfield: 0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
In-Flight Piece 30: Index: 81, Length: 10485760 (10.00 MB), Piece Bitfield Length: 80, Piece Bitfield: 0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
In-Flight Piece 31: Index: 82, Length: 10485760 (10.00 MB), Piece Bitfield Length: 80, Piece Bitfield: 0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.hqwc.cn/news/898594.html

如若内容造成侵权/违法违规/事实不符,请联系编程知识网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

汉字和英文字符对齐研究

1. Windows记事本的情况 2. VS Code 的情况 3. VS 2022的情况 和记事本一样, 不再贴图。4. 总结 1) 不同的编辑器,不同的字体,情况不一样2) 中文和英文,需要不同的个数,才能完全对齐 3) 如果中英文字符数,不能达到上面的字数的倍数,无法对齐 4) 对于强迫症,可以通…

python的基本运用(4)——列表、元组和集合

一、列表(list) (1)列表的介绍和定义 1、类型:"list" 2、符号:[ ] 3、定义列表: a、[ ] 通过[ ] 来定义b、通过list 转换(2)列表的运用 1、通过索引添加值(覆盖原来的值)2、append 添加函数3、insert 在指定的索引位添加元素4、extend 连接两个列表5、rem…

2025 release of Visual Studio Code.

February 2025 (version 1.98) 更新后显示发行说明 Update 1.98.1: The update addresses these issues. Update 1.98.2: The update addresses these issues.Welcome to the February 2025 release of Visual Studio Code. There are many updates in this version that we ho…

销售秘籍:直觉与策略,让客户 “起死回生”

在销售领域里,有时候真得靠自己的直觉来行事。 干这行时间久了,直觉就越来越靠谱。说白了,这有点像一种难以言说的感觉;往正式了说,这就是基于行业经验的直觉判断,是一点点积累起来的。 首先得清楚,对于这类客户,别太纠结,毕竟95%的希望都没了,就当作是“希望不大”的…

UART简介

1.0 UART简介在Linux系统中,终端是一种字符型设备,它有多种类型,通常使用tty(Teletype)来简称各种类型的终端设备。对于嵌入式系统而言,最普遍采用的是UART(Universal Asynchronous Receiver / Transmitter )串行端口,日常生活中简称串口。UART(通用异步收发器)是…

拆解米尔RK3576开发板:瑞芯微第二代AIoT平台如何实现高性价比边缘计算?

文章来源:硬件笔记本最近,AI的风刮得是真猛啊!各种AI工具层出不穷,仿佛一夜之间,人工智能就从科幻走进了现实。作为一名硬件工程师,我自然也按捺不住内心的激动,琢磨着怎么把AI和硬件结合起来,搞点有意思的项目。 这不,机会来了!刚好看到国内知名主板厂商米尔电子新推…

博客迁移~

感谢还在关注本博客的各位朋友 (▽) 我新开了一个博客站点,组织内容跟当前站点的风格不同一样,采用知识花园的形式,内容基本上是我 Obsidian 笔记中挑选出比较完善的部分进行发布,同样也会有像本博客一样的文章,今后大部分内容都会发布在新站点,当然也会适当同步一些文章…

安川机器人DX100示教器维修常见故障合集

在现代工业生产中,安川机器人发挥着极为重要的作用。然而,如同任何设备一样,它们也会遭遇故障,尤其是DX100示教器部分。安川机器人维修、YASKAWA机器人维修(YASKAWA是安川的英文品牌名)、工业机器人维修都是保障生产持续进行的关键环节。对安川机器人示教器维修中的常见故…

SpringSecurity5(5-自定义短信、手机验证码)

本文介绍如何在 Spring Security 中自定义验证码和短信验证码校验,包括生成验证码、存储与校验逻辑、过滤器配置及适配认证流程,确保用户安全登录,同时提升灵活性与可扩展性。适用于登录安全加固及自定义认证需求。图形验证码 SpringSecurity 实现的用户名、密码登录是在 Us…

NineData社区版抢先体验,获取无人机、双肩包、充电宝等周边福利

NineData社区版现已正式上线,支持通过 Docker 部署至本地,保障数据安全与操作留存在本地。为庆祝发布,NineData 开启技术体验官征文活动,邀请开发者分享安装、使用及优化建议等经验。活动设置丰厚奖品,包括大疆无人机、高级旅行箱等,并提供多种加分机制,如真实场景分享和…

EtherCAT转Profinet揭秘网关模块促成西门子PLC与伺服电机通讯的协议转换秘诀案例​

一. 案例背景 西门子1200PLC通过捷米特JM-ECTM-PN(EtherCAT转ProfiNet)网关将松下伺服电机(包括不限于型号MHMFO22D1U2M)或EtherCAT协议的其它设备或连接到ProfiNetPLC上,并在正常运行中支持EtherCAT协议。本产品可作为EtherCAT主站,做为西门子S7-1200系列PLC的从站并在监…

vcftools根据个体id提取数据和删除数据

001、vcftools根据个体id提取数据[b20223040323@admin2 test5]$ ls id.list outcome.vcf [b20223040323@admin2 test5]$ cat id.list GMM5 GMM6 GMM7 GMM8 [b20223040323@admin2 test5]$ grep "^#" outcome.vcf | tail -n 1 | cut -f 10- GMM1 GMM2 GMM3 G…