def read_send_queue(self): octets = "" while self.send_queue: octets = self.send_queue[0] if isinstance(octets, basestring): # remove the piece in any case self.send_queue.popleft() if octets: break else: octets = octets.read(MAXBUF) if octets: break # remove the file-like when it is empty self.send_queue.popleft() if octets: if type(octets) == types.UnicodeType: LOG.oops("Received unicode input") octets = octets.encode("utf-8") if self.encrypt: octets = self.encrypt(octets) return octets
def closed(self, exception=None): if self.close_complete: return self.close_complete = True if exception: LOG.error("* Connection %s: %s" % (self.logname, exception)) elif self.eof: LOG.debug("* Connection %s: EOF" % (self.logname)) else: LOG.debug("* Closed connection %s" % (self.logname)) self.connection_lost(exception) self.parent.connection_lost(self) atclosev, self.atclosev = self.atclosev, set() for func in atclosev: try: func(self, exception) except (KeyboardInterrupt, SystemExit): raise except: LOG.oops("Error in atclosev") if self.measurer: self.measurer.unregister_stream(self) self.send_octets = None self.send_ticks = 0 self.recv_ticks = 0 self.sock.soclose()
def run(poller, conf): """ Start the client or server-side of the negotiate module """ if not "negotiate.listen" in conf: LOG.oops("Thou shall pass 'negotiate.listen' to negotiate") _SERVER = _ServerNegotiate(None) HTTP_SERVER.register_child(_SERVER, "/negotiate/") HTTP_SERVER.register_child(_SERVER, "/collect/")
def unchoke(self, m): btid = _make_btid(m["ident"]) if btid not in AUTH_PEERS: self._streams[m["stream"]] = btid m["stream"].atclose(self._at_close) target_bytes = int(m["request_body"]["target_bytes"]) if target_bytes < 0: raise RuntimeError("Invalid target_bytes") AUTH_PEERS[btid] = {"target_bytes": target_bytes} else: LOG.oops("Multiple negotiation requests")
def run(poller, conf): # Make sure the conf makes sense before we go config.finalize_conf(conf) if conf["bittorrent.listen"]: if conf["bittorrent.negotiate"]: # # We assume that the caller has already started # the HTTP server and that it contains our negotiator # so here we just bring up the test server. # server = ServerPeer(poller) server.configure(conf) server.listen((conf["bittorrent.address"], conf["bittorrent.port"])) else: server = PeerNeubot(poller) server.configure(conf) server.listen((conf["bittorrent.address"], conf["bittorrent.port"])) else: # # Make sure there is someone ready to receive the # "testdone" event. If there is noone it is a bug # none times out of ten. # if not NOTIFIER.is_subscribed("testdone"): LOG.oops("The 'testdone' event is not subscribed") if conf["bittorrent.negotiate"]: client = BitTorrentClient(poller) client.configure(conf) # # The rendezvous client uses this hidden variable # to pass us the URI to connect() to (the rendezvous # returns an URI, not address and port). # uri = None if "bittorrent._uri" in conf: uri = conf["bittorrent._uri"] client.connect_uri(uri) else: client = PeerNeubot(poller) client.configure(conf) client.connect((conf["bittorrent.address"], conf["bittorrent.port"]))
def _send_response(self, m): try: m["parent"].send_response(m) except (KeyboardInterrupt, SystemExit): raise except Exception, e: LOG.oops("send_response() failed: %s" % str(e)) try: m["stream"].close() except (KeyboardInterrupt, SystemExit): raise except Exception, e: LOG.oops("close() failed: %s" % str(e))
def collect_allowed(m): ''' Returns True if we are allowed to collect a result into the database, and False otherwise ''' if type(m) != types.DictType: # # XXX This is a shame therefore put the oops() and hope that # it does its moral suasion job as expected. # LOG.oops("TODO: please pass me a dictionary!", LOG.debug) m = m.__dict__ return (not utils.intify(m["privacy_informed"]) or utils.intify(m["privacy_can_collect"]))
LOG.warning("testing neubot logger -- This is an warning message") LOG.info("testing neubot logger -- This is an info message") LOG.debug("testing neubot logger -- This is a debug message") print compat.json.dumps(LOG.listify()) try: raise Exception("Testing LOG.exception") except (KeyboardInterrupt, SystemExit): raise except: LOG.exception() LOG.exception(func=LOG.warning) LOG.start("Testing the in-progress feature") LOG.progress("...") LOG.progress() LOG.complete("success!") LOG.oops("Testing the new oops feature") # Testing variadic args logging.warning("WARNING %s", "variadic warning") LOG.warning("WARNING %s", "variadic warning") LOG.redirect() LOG.error("testing neubot logger -- This is an error message") LOG.warning("testing neubot logger -- This is an warning message") LOG.info("testing neubot logger -- This is an info message") LOG.debug("testing neubot logger -- This is a debug message")
def atclose(self, func): if func in self.atclosev: LOG.oops("Duplicate atclose(): %s" % func) self.atclosev.add(func)
def compose(self, **kwargs): ''' Prepare a request on the client side ''' self.method = kwargs.get("method", "") if kwargs.get("uri", ""): self.uri = kwargs.get("uri", "") (self.scheme, self.address, self.port, self.pathquery) = urlsplit(self.uri) self["host"] = self.address + ":" + self.port else: self.scheme = kwargs.get("scheme", "") self.address = kwargs.get("address", "") self.port = kwargs.get("port", "") self.pathquery = kwargs.get("pathquery", "") if self.method: # # "A client MUST include a Host header field in all HTTP/1.1 # request messages. If the requested URI does not include # an Internet host name for the service being requested, then # the Host header field MUST be given with an empty value." # -- RFC 2616 # self["host"] = kwargs.get("host", "") if not self["host"]: LOG.oops("Missing host header") self.code = kwargs.get("code", "") self.reason = kwargs.get("reason", "") self.protocol = kwargs.get("protocol", "HTTP/1.1") if kwargs.get("nocache", True): if self.method: self["pragma"] = "no-cache" self["cache-control"] = "no-cache" if kwargs.get("date", True): self["date"] = email.utils.formatdate(usegmt=True) if not kwargs.get("keepalive", True): self["connection"] = "close" # # Curl(1) does not enter into up_to_eof mode unless a # content-type is specified, for this reason I've added # an OOPS below. # TODO Looking again at RFC2616 after adding this bits # realized that another patch is due to make the code # that deals with body and length better. # if kwargs.get("up_to_eof", False): if not "mimetype" in kwargs: LOG.oops("up_to_eof without mimetype") self["content-type"] = kwargs.get("mimetype", "text/plain") elif kwargs.get("body", None): self.body = kwargs.get("body", None) if isinstance(self.body, basestring): self.length = len(self.body) else: utils.safe_seek(self.body, 0, os.SEEK_END) self.length = self.body.tell() utils.safe_seek(self.body, 0, os.SEEK_SET) self["content-length"] = str(self.length) if kwargs.get("mimetype", ""): self["content-type"] = kwargs.get("mimetype", "") elif kwargs.get("chunked", None): self.body = kwargs.get("chunked", None) self.length = -1 self["transfer-encoding"] = "chunked" if kwargs.get("mimetype", ""): self["content-type"] = kwargs.get("mimetype", "") else: self["content-length"] = "0" self.family = kwargs.get("family", socket.AF_INET)
def compose(self, **kwargs): self.method = kwargs.get("method", "") if kwargs.get("uri", ""): self.uri = kwargs.get("uri", "") (self.scheme, self.address, self.port, self.pathquery) = urlsplit(self.uri) self["host"] = self.address + ":" + self.port else: self.scheme = kwargs.get("scheme", "") self.address = kwargs.get("address", "") self.port = kwargs.get("port", "") self.pathquery = kwargs.get("pathquery", "") if self.method: # # "A client MUST include a Host header field in all HTTP/1.1 # request messages. If the requested URI does not include # an Internet host name for the service being requested, then # the Host header field MUST be given with an empty value." # -- RFC 2616 # self["host"] = kwargs.get("host", "") if not self["host"]: LOG.oops("Missing host header") self.code = kwargs.get("code", "") self.reason = kwargs.get("reason", "") self.protocol = kwargs.get("protocol", "HTTP/1.1") if kwargs.get("nocache", True): if self.method: self["pragma"] = "no-cache" self["cache-control"] = "no-cache" if kwargs.get("date", True): self["date"] = date() if not kwargs.get("keepalive", True): self["connection"] = "close" if kwargs.get("body", None): self.body = kwargs.get("body", None) if isinstance(self.body, basestring): self.length = len(self.body) else: utils.safe_seek(self.body, 0, os.SEEK_END) self.length = self.body.tell() utils.safe_seek(self.body, 0, os.SEEK_SET) self["content-length"] = str(self.length) if kwargs.get("mimetype", ""): self["content-type"] = kwargs.get("mimetype", "") elif kwargs.get("chunked", None): self.body = kwargs.get("chunked", None) self.length = -1 self["transfer-encoding"] = "chunked" if kwargs.get("mimetype", ""): self["content-type"] = kwargs.get("mimetype", "") else: self["content-length"] = "0" self.family = kwargs.get("family", socket.AF_INET)