From 8c15c3c2e45dc2412bf2514cebecd19b220876d8 Mon Sep 17 00:00:00 2001 From: Andrew Williams Date: Thu, 26 Nov 2009 17:25:26 +0000 Subject: [PATCH] Further work on output_rtorrent --- output_rtorrent.py | 60 +++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 57 insertions(+), 3 deletions(-) diff --git a/output_rtorrent.py b/output_rtorrent.py index 189cd5f..c799935 100644 --- a/output_rtorrent.py +++ b/output_rtorrent.py @@ -1,6 +1,8 @@ """ Output rTorrent - Feeds selected entries into rTorrent """ import logging from flexget.plugin import * +from urlparse import uses_netloc + __pychecker__ = 'unusednames=parser' @@ -42,10 +44,62 @@ class OutputRtorrent: switch proto: case "file": - s = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM) - s.connect(path) - return s + case "http": raise PluginError("http xmlrpc interface not supported") else: raise PluginError("Unsupported path protocol: %s" % proto) + + +class ScgiTransport(xmlrpclib.Transport): + + """ SGI Transport + + Used to communicate xmlrpc over raw SCGI services, like rTorrent """ + + def __init__(self, use_datetime=0): + self.headers=[] + + def _make_headers(self, headers): + """ Generate the request headers """ + return '\x00'.join(['%s\x00%s'%t for t in headers])+'\x00' + + def _add_header(self, header, data): + if not self.headers: + self.headers = [('SCGI', '1'),] + self.headers.append((header, data)) + + def _gen_netstring(self, data): + return '%d:%s,' % (len(data), data) + + def _gen_request(self, data): + """ Generates the full SCGI request """ + headers = [("CONTENT_LENGTH", len(data))] + headers += [('SCGI', '1')] + rheaders = self._make_headers( headers.extend(self.headers) ) + + return self._gen_netstring(rheaders) + data + + def make_connection(self, url): + uses_netloc.append('unix') + scheme, netloc, path, query, frag = urlparse.urlsplit(url) + host, port = urllib.splitport(netloc) + + if netloc: + addrinfo = socket.getaddrinfo(host, port, socket.AF_INET, socket.SOCK_STREAM) + + sock = socket.socket(*addrinfo[0][:3]) + sock.connect(addrinfo[0][4]) + else: + sock = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM) + sock.connect(path) + + return sock + + def send_request(self, connection, handler, request_body): + + pass + + def send_host(self, connection, host): + pass +