python - 使用 Web api 添加 torrent

标签 python python-requests utorrent

我正在尝试使用 Python 中的 Requests 库通过 uTorrent Web api ( http://www.utorrent.com/community/developers/webapi ) 添加 torrent。

import requests
import re

UTORRENT_URL = 'http://%s:%s/gui/' % ('localhost', '55655')
UTORRENT_URL_TOKEN = '%stoken.html' % UTORRENT_URL
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':'C:\\x.torrent'}
r = requests.post(UTORRENT_URL, auth=auth, cookies=cookies, headers=headers, params=params, files=files)
print r.json()

错误 - 表单参数中未提供 torrent 文件内容

感谢任何帮助

最佳答案

好的,问题出在设置标题。删除它们,它终于起作用了!

import requests
import re

UTORRENT_URL = 'http://%s:%s/gui/' % ('192.168.1.80', '55655')
UTORRENT_URL_TOKEN = '%stoken.html' % UTORRENT_URL
REGEX_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)

关于python - 使用 Web api 添加 torrent,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20827861/

相关文章:

tcp - uTorrent 如何在 TCP 和 uTP 之间进行选择?

python - 如何在django框架内导入或调用HTML中的PY代码

python - cqlengine (Cassandra 2.0) 上的错误时间戳

Python 请求 : URL with percent character

python - 如何使用 Python Requests 模块在 Moodle Rest WS 中创建用户?

python - 使用 pywinauto 的 uTorrent 自动化

python - Seaborn 热图不断在数据集上循环添加颜色条

python - 如何从span类获取content属性的值

python - 使用 python 请求将 XML 文件发送到 Rest API

bittorrent - 如何创建/构建/构建完全无跟踪器的 p2p(点对点)?