def c14n(node): s = "<" + node.name if node.namespace: if not node.parent or node.parent.namespace != node.namespace: s = s + ' xmlns="%s"' % node.namespace sorted_attrs = sorted(node.attrs.keys()) for key in sorted_attrs: val = ustr(node.attrs[key]) # like XMLescape() but with whitespace and without > s = s + ' %s="%s"' % (key, normalise_attr(val)) s = s + ">" cnt = 0 if node.kids: for a in node.kids: if (len(node.data) - 1) >= cnt: s = s + normalise_text(node.data[cnt]) s = s + c14n(a) cnt = cnt + 1 if (len(node.data) - 1) >= cnt: s = s + normalise_text(node.data[cnt]) if not node.kids and s.endswith(">"): s = s[:-1] + " />" else: s = s + "</" + node.name + ">" return s.encode("utf-8")
def send(self,raw_data,retry_timeout=1): """ Writes raw outgoing data. Blocks until done. If supplied data is unicode string, encodes it to utf-8 before send.""" if type(raw_data)==type(u''): raw_data = raw_data.encode('utf-8') elif type(raw_data)<>type(''): raw_data = ustr(raw_data).encode('utf-8') try: sent = 0 while not sent: try: self._send(raw_data) sent = 1 except socket.sslerror, e: if e[0]==socket.SSL_ERROR_WANT_READ: sys.exc_clear() self.DEBUG("SSL_WANT_READ while sending data, wating to retry",'warn') select.select([self._sock],[],[],retry_timeout) continue if e[0]==socket.SSL_ERROR_WANT_WRITE: sys.exc_clear() self.DEBUG("SSL_WANT_WRITE while sending data, waiting to retry",'warn') select.select([],[self._sock],[],retry_timeout) continue raise # Avoid printing messages that are empty keepalive packets. if raw_data.strip(): self.DEBUG(raw_data,'sent') if hasattr(self._owner, 'Dispatcher'): # HTTPPROXYsocket will send data before we have a Dispatcher self._owner.Dispatcher.Event('', DATA_SENT, raw_data)
def c14n(node): s = "<" + node.name if node.namespace: if not node.parent or node.parent.namespace != node.namespace: s = s + ' xmlns="%s"' % node.namespace sorted_attrs = sorted(node.attrs.keys()) for key in sorted_attrs: val = ustr(node.attrs[key]) # like XMLescape() but with whitespace and without > s = s + ' %s="%s"' % (key, normalise_attr(val)) s = s + ">" cnt = 0 if node.kids: for a in node.kids: if (len(node.data) - 1) >= cnt: s = s + normalise_text(node.data[cnt]) s = s + c14n(a) cnt = cnt + 1 if (len(node.data) - 1) >= cnt: s = s + normalise_text(node.data[cnt]) if not node.kids and s.endswith('>'): s = s[:-1] + ' />' else: s = s + "</" + node.name + ">" return s.encode('utf-8')
def send(self, raw_data, retry_timeout=1): """ Writes raw outgoing data. Blocks until done. If supplied data is unicode string, encodes it to utf-8 before send.""" if type(raw_data) == type(u''): raw_data = raw_data.encode('utf-8') elif type(raw_data) <> type(''): raw_data = ustr(raw_data).encode('utf-8') try: sent = 0 while not sent: try: self._send(raw_data) sent = 1 except socket.sslerror, e: if e[0] == socket.SSL_ERROR_WANT_READ: sys.exc_clear() self.DEBUG( "SSL_WANT_READ while sending data, wating to retry", 'warn') select.select([self._sock], [], [], retry_timeout) continue if e[0] == socket.SSL_ERROR_WANT_WRITE: sys.exc_clear() self.DEBUG( "SSL_WANT_WRITE while sending data, waiting to retry", 'warn') select.select([], [self._sock], [], retry_timeout) continue raise # Avoid printing messages that are empty keepalive packets. if raw_data.strip(): self.DEBUG(raw_data, 'sent') if hasattr( self._owner, 'Dispatcher' ): # HTTPPROXYsocket will send data before we have a Dispatcher self._owner.Dispatcher.Event('', DATA_SENT, raw_data)
def encode_stanza(self, stanza): """ Encode str or unicode to utf-8 """ if isinstance(stanza, unicode): stanza = stanza.encode('utf-8') elif not isinstance(stanza, str): stanza = ustr(stanza).encode('utf-8') return stanza
def send(self,raw_data): """Writes raw outgoing data. Blocks until done. If supplied data is unicode string, treating it as utf-8 encoded.""" if type(raw_data)==type(u''): raw_data = raw_data.encode('utf-8') elif type(raw_data)<>type(''): raw_data = ustr(raw_data).encode('utf-8') try: self._send(raw_data) self.DEBUG(raw_data,'sent') except: self.DEBUG("Socket error while sending data",'error') self._owner.disconnected()
def send(self, raw_data): """Writes raw outgoing data. Blocks until done. If supplied data is unicode string, treating it as utf-8 encoded.""" if type(raw_data) == type(u''): raw_data = raw_data.encode('utf-8') elif type(raw_data) <> type(''): raw_data = ustr(raw_data).encode('utf-8') try: self._send(raw_data) self.DEBUG(raw_data, 'sent') except: self.DEBUG("Socket error while sending data", 'error') self._owner.disconnected()
def send(self, raw_data, retry_timeout=0): """ Writes raw outgoing data. Blocks until done. If supplied data is unicode string, encodes it to utf-8 before send.""" if type(raw_data) == type(u''): data_to_send = raw_data.encode('utf-8') elif type(raw_data) <> type(''): data_to_send = ustr(raw_data).encode('utf-8') else: data_to_send = raw_data left_to_send = len(data_to_send) total_sent = 0 retry = 0 try: while (left_to_send): try: if retry == 0: data = data_to_send[total_sent:] bytes_sent = self._send(data) total_sent += bytes_sent left_to_send -= bytes_sent except socket.sslerror, e: self.DEBUG( "Exception %s while sending %s / %s bytes" % (e, total_sent, left_to_send), 'warn') if e[0] == socket.SSL_ERROR_WANT_READ: retry += 1 sys.exc_clear() self.DEBUG( "SSL_WANT_READ while sending data, waiting to retry (%s)" % (retry), 'warn') select.select([self._sock], [], [], retry_timeout) continue if e[0] == socket.SSL_ERROR_WANT_WRITE: retry += 1 sys.exc_clear() self.DEBUG( "SSL_WANT_WRITE while sending data, waiting to retry (%s)" % (retry), 'warn') select.select([], [self._sock], [], retry_timeout) continue raise if not retry == 0: self.DEBUG( "Finally sent %s bytes of data after %s retries" % (total_sent, retry), 'warn') retry = 0 # Avoid printing messages that are empty keepalive packets. if data_to_send.strip(): self.DEBUG(data_to_send, 'sent') if hasattr( self._owner, 'Dispatcher' ): # HTTPPROXYsocket will send data before we have a Dispatcher self._owner.Dispatcher.Event('', DATA_SENT, data_to_send)
def send(self,raw_data): """ Writes raw outgoing data. Blocks until done. If supplied data is unicode string, encodes it to utf-8 before send.""" if type(raw_data)==type(u''): raw_data = raw_data.encode('utf-8') elif type(raw_data)<>type(''): raw_data = ustr(raw_data).encode('utf-8') try: self._send(raw_data) # Avoid printing messages that are empty keepalive packets. if raw_data.strip(): self.DEBUG(raw_data,'sent') self._owner.Dispatcher.Event('', DATA_SENT, raw_data) except: self.DEBUG("Socket error while sending data",'error') somev=0; #self._owner.disconnected() # временная мера //PG, 13.07.2007
def send(self,raw_data): """ Writes raw outgoing data. Blocks until done. If supplied data is unicode string, encodes it to utf-8 before send.""" if type(raw_data)==type(u''): raw_data = raw_data.encode('utf-8') elif type(raw_data)<>type(''): raw_data = ustr(raw_data).encode('utf-8') try: self._send(raw_data) # Avoid printing messages that are empty keepalive packets. if raw_data.strip(): self.DEBUG(raw_data,'sent') self._owner.Dispatcher.Event('', DATA_SENT, raw_data) except: self.DEBUG("Socket error while sending data",'error') self._owner.disconnected()
def send(self,raw_data): """ Writes raw outgoing data. Blocks until done. If supplied data is unicode string, encodes it to utf-8 before send.""" if isinstance(raw_data, unicode): raw_data = raw_data.encode('utf-8') elif not isinstance(raw_data, str): raw_data = ustr(raw_data).encode('utf-8') try: self._send(raw_data) # Avoid printing messages that are empty keepalive packets. if raw_data.strip(): self.DEBUG(raw_data,'sent') self._owner.Dispatcher.Event('', DATA_SENT, raw_data) except Exception: self.DEBUG("Socket error while sending data",'error') self._owner.disconnected()
def send(self,raw_data): """ Writes raw outgoing data. Blocks until done. If supplied data is unicode string, encodes it to utf-8 before send.""" if type(raw_data)==type(u''): raw_data = raw_data.encode('utf-8') elif type(raw_data)<>type(''): raw_data = ustr(raw_data).encode('utf-8') try: self._send(raw_data) # Avoid printing messages that are empty keepalive packets. if raw_data.strip(): self.DEBUG(raw_data,'sent') if hasattr(self._owner, 'Dispatcher'): # HTTPPROXYsocket will send data before we have a Dispatcher self._owner.Dispatcher.Event('', DATA_SENT, raw_data) except: self.DEBUG("Socket error while sending data",'error') self._owner.disconnected()
def send(self, raw_data): """ Writes raw outgoing data. Blocks until done. If supplied data is unicode string, encodes it to utf-8 before send.""" if type(raw_data) == type(u""): raw_data = raw_data.encode("utf-8") elif type(raw_data) <> type(""): raw_data = ustr(raw_data).encode("utf-8") try: self._send(raw_data) # Avoid printing messages that are empty keepalive packets. if raw_data.strip(): self.DEBUG(raw_data, "sent") self._owner.Dispatcher.Event("", DATA_SENT, raw_data) except: self.DEBUG("Socket error while sending data", "error") self._owner.disconnected()
def send(self, raw_data): """ Writes raw outgoing data. Blocks until done. If supplied data is unicode string, encodes it to utf-8 before send.""" if isinstance(raw_data, unicode): raw_data = raw_data.encode('utf-8') elif not isinstance(raw_data, str): raw_data = ustr(raw_data).encode('utf-8') try: self._send(raw_data) # Avoid printing messages that are empty keepalive packets. if raw_data.strip(): self.DEBUG(raw_data, 'sent') if hasattr( self._owner, 'Dispatcher' ): # HTTPPROXYsocket will send data before we have a Dispatcher self._owner.Dispatcher.Event('', DATA_SENT, raw_data) except: self.DEBUG("Socket error while sending data", 'error') self._owner.disconnected()
def send(self, raw_data, now = False): '''Append raw_data to the queue of messages to be send. If supplied data is unicode string, encode it to utf-8. ''' if self.state <= 0: return r = raw_data if isinstance(r, unicode): r = r.encode('utf-8') elif not isinstance(r, str): r = ustr(r).encode('utf-8') if now: self.sendqueue.insert(0, r) self._do_send() else: self.sendqueue.append(r) self._plug_idle()
def send(self,raw_data,retry_timeout=0): """ Writes raw outgoing data. Blocks until done. If supplied data is unicode string, encodes it to utf-8 before send.""" if type(raw_data)==type(u''): data_to_send = raw_data.encode('utf-8') elif type(raw_data)<>type(''): data_to_send = ustr(raw_data).encode('utf-8') else: data_to_send = raw_data left_to_send = len(data_to_send) total_sent = 0 retry = 0 try: while (left_to_send): try: if retry == 0: data = data_to_send[total_sent:] bytes_sent = self._send(data) total_sent += bytes_sent left_to_send -= bytes_sent except socket.sslerror, e: self.DEBUG("Exception %s while sending %s / %s bytes" % (e, total_sent, left_to_send), 'warn') if e[0]==socket.SSL_ERROR_WANT_READ: retry +=1 sys.exc_clear() self.DEBUG("SSL_WANT_READ while sending data, waiting to retry (%s)" % (retry),'warn') select.select([self._sock],[],[],retry_timeout) continue if e[0]==socket.SSL_ERROR_WANT_WRITE: retry +=1 sys.exc_clear() self.DEBUG("SSL_WANT_WRITE while sending data, waiting to retry (%s)" % (retry),'warn') select.select([],[self._sock],[],retry_timeout) continue raise if not retry == 0: self.DEBUG("Finally sent %s bytes of data after %s retries" % (total_sent, retry), 'warn') retry = 0 # Avoid printing messages that are empty keepalive packets. if data_to_send.strip(): self.DEBUG(data_to_send,'sent') if hasattr(self._owner, 'Dispatcher'): # HTTPPROXYsocket will send data before we have a Dispatcher self._owner.Dispatcher.Event('', DATA_SENT, data_to_send)
def send(self,rawdata): if isinstance(rawdata, unicode): rawdata = rawdata.encode("utf-8") elif not isinstance(rawdata, str): rawdata = ustr(rawdata).encode("utf-8") func(self,rawdata)