1

I want to download .torrent file from the download link . I want the said file to be saved in .torrent format in the project folder I've tried the following code

import urllib.request
url = 'https://torcache.net/torrent/92B4D5EA2D21BC2692A2CB1E5B9FBECD489863EC.torrent?title=[kat.cr]avengers.age.of.ultron.2015.1080p.brrip.x264.yify'

def download_torrent(url):
    name= "movie"
    full_name = str(name) + ".torrent"
    urllib.request.urlretrieve(url, full_name)


download_torrent(url)

It shows the following error:

Traceback (most recent call last):
  File "/home/taarush/PycharmProjects/untitled/fkn.py", line 10, in <module>
    download_torrent(url)
  File "/home/taarush/PycharmProjects/untitled/fkn.py", line 7, in download_torrent
    urllib.request.urlretrieve(url, full_name)
  File "/usr/lib/python3.5/urllib/request.py", line 187, in urlretrieve
    with contextlib.closing(urlopen(url, data)) as fp:
  File "/usr/lib/python3.5/urllib/request.py", line 162, in urlopen
    return opener.open(url, data, timeout)
  File "/usr/lib/python3.5/urllib/request.py", line 465, in open
    response = self._open(req, data)
  File "/usr/lib/python3.5/urllib/request.py", line 483, in _open
    '_open', req)
  File "/usr/lib/python3.5/urllib/request.py", line 443, in _call_chain
    result = func(*args)
  File "/usr/lib/python3.5/urllib/request.py", line 1286, in https_open
    context=self._context, check_hostname=self._check_hostname)
  File "/usr/lib/python3.5/urllib/request.py", line 1246, in do_open
    r = h.getresponse()
  File "/usr/lib/python3.5/http/client.py", line 1197, in getresponse
    response.begin()
  File "/usr/lib/python3.5/http/client.py", line 297, in begin
    version, status, reason = self._read_status()
  File "/usr/lib/python3.5/http/client.py", line 266, in _read_status
    raise RemoteDisconnected("Remote end closed connection without"
http.client.RemoteDisconnected: Remote end closed connection without response

Where did it go wrong? is there a way to use magnet links? (urllib library only)

Taarush V
  • 15
  • 8
  • 1
    Most likely you need to supply additional headers like User-Agent and others so your request looks like it was made by a browser – Konstantin May 12 '16 at 08:36
  • @Alik http://stackoverflow.com/questions/802134/changing-user-agent-on-urllib2-urlopen . This question is helpful but i can't understand where i should add the header in my code. – Taarush V May 12 '16 at 08:57

1 Answers1

0

Add User-Agent http header as @Alik suggested. The question you've linked shows how. Here's an adaptation for Python 3:

#!/usr/bin/env python3
import urllib.request

opener = urllib.request.build_opener()
opener.addheaders = [('User-Agent', 'CERN-LineMode/2.15 libwww/2.17b3')]
urllib.request.install_opener(opener) #NOTE: global for the process

urllib.request.urlretrieve(url, filename)

See the specification for User-Agent. If the site rejects your custom User-Agent, you could send User-Agent produced by ordinary browsers such as fake_useragent.UserAgent().random.

Community
  • 1
  • 1
jfs
  • 399,953
  • 195
  • 994
  • 1,670