Created
January 27, 2012 15:14
-
-
Save Elwell/1689232 to your computer and use it in GitHub Desktop.
Transmission statisics via RPC/json/python
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/python | |
# ideas lifted from bash collectd Plugin to monitor transmission-daemon traffic | |
# Written by Yarek T, however that was old and didn't play nice :-) | |
# Andrew Elwell. Jan 2012 | |
from urllib2 import Request, urlopen, URLError, HTTPError | |
import json | |
# see https://trac.transmissionbt.com/browser/trunk/extras/rpc-spec.txt | |
endpoint = "http://localhost:9091/transmission/rpc" | |
sessid = '' | |
req = Request(endpoint) | |
req.add_header('x-transmission-session-id', sessid) | |
try: response = urlopen(req) | |
except HTTPError, e: | |
# The 1st time this WILL fail as we haven't got session-id | |
if (e.code == 409): # grab session-id | |
sessid = e.hdrs['x-transmission-session-id'] | |
print "Updated session-id to %s" % sessid | |
else: | |
print 'The server couldn\'t fulfill the request.' | |
print 'Error code: ', e.code | |
except URLError, e: | |
print 'Failed to reach server.' | |
print 'Reason: ', e.reason | |
# construct the JSON string | |
data = json.dumps({'method':'session-stats'}) | |
# this time with feeling... | |
req = Request(endpoint) | |
req.add_header('x-transmission-session-id', sessid) | |
response = urlopen(req,data) | |
print json.dumps(json.loads(response.read()),indent=4) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment