def run(poller, conf): ''' This function is invoked when Neubot is already running and you want to leverage some functionalities of this module. ''' # 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 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: oops("Received unicode input") octets = octets.encode("utf-8") return octets
if __name__ == "__main__": logging.info("INFO w/ logging.info") logging.debug("DEBUG w/ logging.debug") logging.warning("WARNING w/ logging.warning") logging.error("ERROR w/ logging.error") print compat.json.dumps(LOG.listify()) access_logger = logging.getLogger('access') access_logger.info('Test access logger') try: raise Exception("Testing exc_info") except (KeyboardInterrupt, SystemExit): raise except: logging.error('EXCEPTION', exc_info=1) oops("Testing the new oops feature") # Testing variadic args logging.warning("WARNING %s", "variadic warning") LOG.redirect() logging.info("INFO w/ logging.info") logging.debug("DEBUG w/ logging.debug") logging.warning("WARNING w/ logging.warning") logging.error("ERROR w/ logging.error")
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"]: 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: 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 atclose(self, func): if func in self.atclosev: oops("Duplicate atclose(): %s" % func) self.atclosev.add(func)