Jump to content

Add torrent error (Python)


Recommended Posts

I'm trying to add a torrent with uTorrent web api, in Python using Requests library, but this error is returned:

Error - torrent file content not supplied in form parameter

import requests, re

UTORRENT_URL = 'http://%s:%s/gui/' % ('localhost', '55655')
REGEX_UTORRENT_TOKEN = r'<div[^>]*id=[\"\']token[\"\'][^>]*>([^<]*)</div>'

auth = requests.auth.HTTPBasicAuth('x', 'x')
headers = {'content-type': 'application/json'}
r = requests.get(UTORRENT_URL_TOKEN, auth=auth, headers=headers)
token = re.search(REGEX_UTORRENT_TOKEN, r.text).group(1)
guid = r.cookies['GUID']
cookies = dict(GUID = guid)

headers = {'content-type': 'multipart/form-data'}
params = {'action':'add-file','token': token}
files = {'torrent_file': open('C:\\x.torrent', 'rb').read()}
r = requests.post(UTORRENT_URL, auth=auth, cookies=cookies, headers=headers, params=params, files=files)
print r.json()

Link to comment
Share on other sites

  • 2 weeks later...

Ok, the problem was setting the headers. Removing them and it finally works!

import requestsimport reUTORRENT_URL = 'http://%s:%s/gui/' % ('', '55655')UTORRENT_URL_TOKEN = '%stoken.html' % UTORRENT_URLREGEX_UTORRENT_TOKEN = r'<div[^>]*id=[\"\']token[\"\'][^>]*>([^<]*)</div>'auth = requests.auth.HTTPBasicAuth('x', 'x')r = requests.get(UTORRENT_URL_TOKEN, auth=auth)token = re.search(REGEX_UTORRENT_TOKEN, r.text).group(1)guid = r.cookies['GUID']cookies = dict(GUID = guid)params = {'action':'add-file','token': token}files = {'torrent_file': open('C:\\x.torrent', 'rb')}r = requests.post(url=UTORRENT_URL, auth=auth, cookies=cookies, params=params, files=files)
Link to comment
Share on other sites


This topic is now archived and is closed to further replies.

  • Create New...