0

このコードを使用して .torrent ファイルをダウンロードしています:

torrent = urllib2.urlopen(torrent URL, timeout = 30)
output = open('mytorrent.torrent', 'wb')
output.write(torrent.read())

結果の mytorrent.torrent ファイルはどの bittorrent クライアントでも開かず、「メタ ファイルを解析できません」というエラーが発生します。どうやら問題は、トレント URL (例: http://torcache.com/torrent-file-1.torrent ) が「.torrent」サフィックスで終わっているにもかかわらず、gzip を使用して圧縮されているため、解凍してから次のように保存する必要があることです。トレントファイル。gunzip mytorrent.torrent > test.torrentターミナルでファイルを解凍し、正常に開くbittorrentクライアントでファイルを開くことで、これを確認しました。

Python を変更してファイルのエンコーディングを調べ、ファイルがどのように圧縮されているかを調べ、適切なツールを使用して圧縮を解除し、.torrent ファイルとして保存するにはどうすればよいですか?

4

1 に答える 1

1

gzip されたデータは解凍する必要があります。content-encoding ヘッダーを調べれば、これを検出できます。

import gzip, urllib2, StringIO

req = urllib2.Request(url)
opener = urllib2.build_opener()
response = opener.open(req)
data = response.read()
if response.info()['content-encoding'] == 'gzip':
    gzipper = gzip.GzipFile(StringIO(fileobj=data))
    plain = gzipper.read()
    data = plain
output.write(data)
于 2013-06-03T12:03:36.370 に答える