def __post_soap_async(self, name, soap, async_cb): def on_soap_response(resp): if (resp and resp.finished()): status = resp.get_status() if (status == 200): values = self.__parse_soap_response(resp.read(), name) else: values = None try: async_cb(status, *values) except: import traceback; traceback.print_exc() #end if length = len(soap) soap_action = "\"" + self.__namespace + "#" + name + "\"" endpoint = network.URL(self.__endpoint) conn = HTTPConnection(endpoint.host, endpoint.port or 80) conn.putrequest("POST", endpoint.path) conn.putheader("Host", endpoint.port and "%s:%d" % (endpoint.host, endpoint.port) or host) conn.putheader("User-Agent", "MediaBox") conn.putheader("Content-Type", "text/xml; charset=\"utf-8\"") conn.putheader("Content-Length", str(length)) conn.putheader("SOAPAction", soap_action) conn.endheaders() conn.send(soap, on_soap_response)
def __init__(self, url, cb, *args): # location history for avoiding redirect loops self.__location_history = [] addr = network.URL(url) HTTPConnection.__init__(self, addr.host, addr.port) logging.debug("[downloader] retrieving: %s", url) if (url.startswith("/")): t = threading.Thread(target=self.__get_local_file, args=[url, cb, args]) t.setDaemon(True) t.start() else: if (addr.query_string): path = addr.path + "?" + addr.query_string else: path = addr.path self.putrequest("GET", path, "HTTP/1.1") self.putheader("Host", "%s:%d" % (addr.host, addr.port)) self.putheader("User-Agent", "MediaBox") self.putheader("Connection", "close") self.endheaders() self.send("", self.__on_receive_data, cb, args)
def __renew_subscription(self, sid): """ Timeout handler for renewing a subscription. """ del self.__renewal_handlers[sid] ev_url = self.__sid_to_url[sid] logging.debug("[upnp gena] renewing subscription for: %s", ev_url) addr = network.URL(ev_url) conn = HTTPConnection(addr.host, addr.port) conn.send_raw(_GENA_RENEW % (addr.path, addr.host, sid), self.__on_receive_confirmation, ev_url)
def __on_receive_data(self, resp, cb, args): if (not resp): cb(None, 0, 0, *args) return status = resp.get_status() if (status == 200): amount, total = resp.get_amount() data = resp.read() if (data): #print data cb(data, amount, total, *args) if (resp.finished()): cb("", amount, total, *args) elif (300 <= status < 310): location = resp.get_header("LOCATION") if (not location in self.__location_history): self.__location_history.append(location) addr = network.URL(location) logging.debug("[downloader] HTTP redirect to: %s" % location) if (addr.query_string): path = addr.path + "?" + addr.query_string else: path = addr.path self.redirect(addr.host, addr.port) self.putrequest("GET", path, "HTTP/1.1") self.putheader("Host", "%s:%d" % (addr.host, addr.port)) self.putheader("User-Agent", "MediaBox") self.putheader("Connection", "close") self.endheaders() self.send("", self.__on_receive_data, cb, args) else: self.__location_history.append(location) logging.error("[downloader] redirect loop detected:\n%s", "\n-> ".join(self.__location_history)) cb(None, 0, 0, *args) return elif (400 <= status < 510): cb(None, 0, 0, *args) return
def subscribe(self, ev_url, cb): """ Subscribes the given callback to the given event URL. @since: 0.96 """ # create GENA socket when first used if (not self.__gena_socket): self.__gena_socket = self.__create_gena_socket() # only subscribe once per event URL if (ev_url in self.__url_to_sid): sid = self.__url_to_sid[ev_url] self.__handlers[sid].append(cb) logging.debug("[upnp gena] subscribing to: %s", ev_url) addr = network.URL(ev_url) conn = HTTPConnection(addr.host, addr.port) conn.send_raw(_GENA_SUBSCRIBE % (addr.path, addr.host, self.__gena_url), self.__on_receive_confirmation, ev_url, cb)
def unsubscribe(self, cb): """ Unsubscribes the given callback. @since: 0.96 """ if (cb in self.__cb_to_sid): sid = self.__cb_to_sid[cb] ev_url = self.__sid_to_url[sid] addr = network.URL(ev_url) conn = HTTPConnection(addr.host, addr.port) conn.send_raw(_GENA_UNSUBSCRIBE % (addr.path, addr.host, sid), lambda a,b:True) self.__handlers[sid].remove(cb) if (not self.__handlers[sid]): del self.__handlers[sid] del self.__sid_to_url[sid] del self.__url_to_sid[ev_url] del self.__renewal_handlers[sid] del self.__cb_to_sid[cb]
def __post_soap(self, name, soap): length = len(soap) soap_action = "\"" + self.__namespace + "#" + name + "\"" endpoint = network.URL(self.__endpoint) conn = httplib.HTTPConnection(endpoint.host, endpoint.port or 80) conn.putrequest("POST", endpoint.path) conn.putheader("Host", endpoint.port and "%s:%d" % (endpoint.host, endpoint.port) or endpoint.host) conn.putheader("User-Agent", "MediaBox") conn.putheader("Content-Type", "text/xml; charset=\"utf-8\"") conn.putheader("Content-Length", str(length)) conn.putheader("SOAPAction", soap_action) conn.endheaders() conn.send(soap) response = conn.getresponse() values = self.__parse_soap_response(response.read(), name) return values
def get_query(self): return network.URL(self._get_headers().path).query
def get_path(self): return network.URL(self._get_headers().path).path
logging.debug("[conn %s] finished", self._get_id()) if (not self.__close_connection): logging.debug("[conn %s] still alive", self._get_id()) if (__name__ == "__main__"): from utils import network import gtk import sys gtk.gdk.threads_init() def f(resp): if (resp): print resp.read() if (resp.finished()): gtk.main_quit() addr = network.URL(sys.argv[1]) conn = HTTPConnection(addr.host, addr.port) conn.putrequest("GET", addr.path) conn.putheader("Host", addr.port and "%s:%d" % (addr.host, addr.port) or addr.host) conn.endheaders() conn.send("", f) gtk.main()