def _read_loop(self, resp): logger.debug("_read_loop init") # retry limit for http connecting http_count = 0 while self.running: if resp.isclosed(): break logger.debug("_readloop start(%s)" % str(resp.status)) # read length data = '' while True: try: c = resp.read(1) if c == '\n': http_count = 0 break data += c except SSLError, exception: logger.info("(%s retry:%d):%s" % ("_read_loop", http_count, str(exception))) http_count += 1 if http_count > self.http_retry: logger.error("over retry max") raise NoResponse("break loop") data = data.strip() # read data and pass into listener if self.listener.on_data(data) is False: self.running = False
def remove_event(self, name): try: index = self.get_event_index(name) del self.events[index] except NotFoundEvent: raise except: raise else: logger.info("Remove event '%s'" % name)
def dispatch(self, status): for ev in self.events: if status.author.screen_name in ev['allowuser']: status.text = guess_decode(status.text) if not ev.has_key('hashtag') or ev['hashtag'].search(status.text): logger.info("callback by %s" % (status.author.screen_name,)) try: ev["callback"](self, status) return except Exception: raise
def dispatch(self, status): for ev in self.events: if status.author.screen_name in ev['allowuser']: status.text = guess_decode(status.text) if not ev.has_key('hashtag') or ev['hashtag'].search( status.text): logger.info("callback by %s" % (status.author.screen_name, )) try: ev["callback"](self, status) return except Exception: raise
def _run(self): # Authenticate url = "%s://%s%s" % (self.scheme, self.host, self.url) # Connect and process the stream error_counter = 0 conn = None exception = None logger.debug(u"run streaming server.") while self.running: # quit if error count greater than retry count if self.retry_count is not None and error_counter > self.retry_count: break try: if self.scheme == "http": conn = httplib.HTTPConnection(self.host) else: conn = httplib.HTTPSConnection(self.host) self.auth.apply_auth(url, 'POST', self.headers, self.parameters) conn.connect() conn.sock.settimeout(self.timeout) conn.request('POST', self.url, self.body, headers=self.headers) logger.debug(u"wait response.") resp = conn.getresponse() logger.debug(u"connect response: %s" % str(resp)) if resp.status != 200: if self.listener.on_error(resp.status) is False: break error_counter += 1 logger.error("response status: error(%s)", (str(resp.status), )) sleep(self.retry_time) else: error_counter = 0 logger.info("response status: ok(%s)", (str(resp.status), )) self._read_loop(resp) except socket.error, err: self.listener.on_socket_error(err) except socket.gaierror, err: self.listener.on_socket_error(err)
def _run(self): # Authenticate url = "%s://%s%s" % (self.scheme, self.host, self.url) # Connect and process the stream error_counter = 0 conn = None exception = None logger.debug(u"run streaming server.") while self.running: # quit if error count greater than retry count if self.retry_count is not None and error_counter > self.retry_count: break try: if self.scheme == "http": conn = httplib.HTTPConnection(self.host) else: conn = httplib.HTTPSConnection(self.host) self.auth.apply_auth(url, 'POST', self.headers, self.parameters) conn.connect() conn.sock.settimeout(self.timeout) conn.request('POST', self.url, self.body, headers=self.headers) logger.debug(u"wait response.") resp = conn.getresponse() logger.debug(u"connect response: %s" % str(resp)) if resp.status != 200: if self.listener.on_error(resp.status) is False: break error_counter += 1 logger.error("response status: error(%s)", (str(resp.status),)) sleep(self.retry_time) else: error_counter = 0 logger.info("response status: ok(%s)", (str(resp.status),)) self._read_loop(resp) except socket.error, err: self.listener.on_socket_error(err) except socket.gaierror, err: self.listener.on_socket_error(err)
error_counter = 0 logger.info("response status: ok(%s)", (str(resp.status), )) self._read_loop(resp) except socket.error, err: self.listener.on_socket_error(err) except socket.gaierror, err: self.listener.on_socket_error(err) except NoResponse, err: self.listener.on_noresponse(err) except timeout: logger.info("response status: timeout") if self.listener.on_timeout() == False: break if self.running is False: break conn.close() sleep(self.snooze_time) except Exception, exception: if self.listener.on_fatal_error(exception): continue raise # any other exception is fatal, so kill loop #break logger.error("(%s):%s" % ("_run", "false"))
def on_noresponse(self, err): logger.info(u"[response error] %s." % err)
def on_timeout(self): logger.info("[timeout]") return True
else: error_counter = 0 logger.info("response status: ok(%s)", (str(resp.status),)) self._read_loop(resp) except socket.error, err: self.listener.on_socket_error(err) except socket.gaierror, err: self.listener.on_socket_error(err) except NoResponse, err: self.listener.on_noresponse(err) except timeout: logger.info("response status: timeout") if self.listener.on_timeout() == False: break if self.running is False: break conn.close() sleep(self.snooze_time) except Exception, exception: if self.listener.on_fatal_error(exception): continue raise # any other exception is fatal, so kill loop #break logger.error("(%s):%s" % ("_run", "false"))