- 간단한 툴을 만들며, 코딩의 자신감을 드리고
- 파이썬으로 토렌트 클라이언트를 만들어 봄으로써, 파이썬에 간단한 개념을 설명하고자 합니다.
- qbittorrent와 연동하는 초간단 방법을 알려드림으로써, 관련 응용법을 도와드리고자 합니다. ^^;
- 파이썬 3.x 설치후 python-qbittorrent 패키지를 다음과 같이 설치해주세요
pip3 install python-qbittorrent
- qBittorrent를 다운 받아 설치해주세요
https://www.qbittorrent.org/download.php
- qBittorrent 클라이언트의 웹 UI 를 활성화 해주세요. 설정방법은 다음과 같습니다.
(1) 메뉴바에서 도구 > Options 를 선택
(2) 다음과 같이 웹 사용자 인터페이스를 체크하고 설정해주세요.
위 설정이 완료되면 127.0.0.1:8080 으로 다음과 같이 qBittorrent 접속이 가능해집니다.
위 로그인 화면에서 초기 로그인 정보를 입력하면 ( the default is admin:adminadmin )
다음과 같은 화면을 볼 수 있다면, 일단 스크립트 이전의 모든 설정은 완료된 것 입니다.
지금까지 잘 따라왔다면 다음부터 파이썬코드를 하나하나 봅시다.
파이썬의 qbittorrent 패키지의 Client를 임포트 해줍니다.
from qbittorrent import Client
파이썬을 이용하여 웹 클라이언트에 접속합니다.
# qbittorent Web 클라이언트 api 접속
qb = Client("http://127.0.0.1:8080/")
# qbittorrent에서 설정한 계정정보를 설정.
qb.login("admin", "adminadmin")
다운받을 토렌트 파일을 읽어 옵니다. (예제는 debian 설치용 iso 파일을 다운 받을 수 있는 토렌트 파일)
torrent_file = open("debian-10.2.0-amd64-netinst.iso.torrent", "rb")
자 이제 다운 받아 봅시다.
qb.download_from_file(torrent_file)
지금까지 정상적으로 실행되었다면..
여러분은 다음처럼 qBittorrent 웹UI에서 다운로드되고 있는 것을 확인할 수 있습니다.
만약 토렌트파일이 아닌 magnet URL을 이용하여 파일을 다운 받고 싶다면..
qb.download_from_file 인자값을 파일대신 다음과 같이 마그네틱주소로 대체해주세요.
magnet_link = "magnet:?xt=urn:btih:e334ab9ddd91c10938a7....."
qb.download_from_link(magnet_link)
음.. 다운로드 경로를 바꾸고 싶어진다면.. 다음과 같이 해보세요.
qb.download_from_file(torrent_file, savepath="/the/path/you/want/to/save")
다운 로드를 모두 중지시키고 싶다면?
qb.pause_all()
중지된 다운로드를 다시 시작시키고 싶다면?
qb.resume_all()
좀 더 고급스럽게 다운로드되고 있는 리스트 정보를 보고 싶다면?
def get_size_format(b, factor=1024, suffix="B"):
for unit in ["", "K", "M", "G", "T", "P", "E", "Z"]:
if b < factor:
return f"{b:.2f}{unit}{suffix}"
b /= factor
return f"{b:.2f}Y{suffix}"
# return list of torrents
torrents = qb.torrents()
for torrent in torrents:
print("Torrent name:", torrent["name"])
print("hash:", torrent["hash"])
print("Seeds:", torrent["num_seeds"])
print("File size:", get_size_format(torrent["total_size"]))
print("Download speed:", get_size_format(torrent["dlspeed"]) + "/s")
위 스크립트가 정상적으로 작동된다면 다음과 같이 보여질 수 있을껍니다.
전체 스크립트는 다음의 링크와 같으며, 아래 그림과 같이 실행할 수 있습니다.
https://github.com/cookyman74/utility/blob/master/qbittorrent_client.py
[파이썬] 주변사람으로부터 야구영상 지키기 (0) | 2021.03.21 |
---|---|
파이썬, URL 단축 패키지 소개 (0) | 2021.03.18 |
댓글 영역