示例#1
0
 def __init__(self, sock):
     self._sock = sock
     self._in_buffer = ByteBuffer()
     self._out_buffer = ByteBuffer()
     self._read_thread = Thread(target=self._recv)
     self._write_thread = Thread(target=self._send)
     self._open = True
     self._read_thread.start()
     self._write_thread.start()
示例#2
0
 def __init__(self, sock, main_interrupt=False):
     self._inter_main = main_interrupt
     self._sock = sock
     self._sock.settimeout(1)
     self._in_buffer = ByteBuffer()
     self._out_buffer = ByteBuffer()
     self._read_thread = Thread(target=self._recv)
     self._write_thread = Thread(target=self._send)
     self._open = True
     self._read_thread.start()
     self._write_thread.start()
示例#3
0
class Client:
    def __init__(self, sock):
        self._sock = sock
        self._in_buffer = ByteBuffer()
        self._out_buffer = ByteBuffer()
        self._read_thread = Thread(target=self._recv)
        self._write_thread = Thread(target=self._send)
        self._open = True
        self._read_thread.start()
        self._write_thread.start()
    def _recv(self):
        '''
        reads constantly into the buffer
        '''
        while self._open:
            self._in_buffer.write_bytes(self._sock.recv(4096))
    def _send(self):
        '''
        writes the out buffer to the socket
        '''
        while self._open:
            self._sock.send(self._out_buffer.flush())
    def get_buffers(self):
        return self._in_buffer, self._out_buffer
    def close(self):
        self._open = False
        self._sock.close()
        self._in_buffer.close()
        self._out_buffer.close()
def tradingviewx(transport):##{
    buffer = ByteBuffer()
    xhr = next(transport)

    while True:
        try:
            data = xhr.raw.read(XHR_READ_SIZE)
        except Exception:
            xhr = next(transport)
            continue

        buffer.write(data)
        line = buffer.read_until(b"\r\n", consume=True)

        if not line: continue
        if line == ": -1": continue
        if line.startswith("data: "): line = line[6:]

        try:
            pkg = jsload(line)
        except Exception:
            continue

        channel = pkg.get("text", {}).get("channel")
        if channel != "chat_bitcoin": continue

        content = pkg.get("text").get("content")
        login = content.get('username')
        msg = content.get("text", "").encode("utf-8", errors="replace")
        meta = content.get("meta", {})
        url = meta.get("url", "").encode("utf-8", errors="replace")
        if url:
            msg = "{}\n{:<19}{}{}{}".format(msg, "", COLOR_10, url, COLOR_0)
        if not msg: continue

        yield(login, msg)
示例#5
0
class Client:
    def __init__(self, sock, main_interrupt=False):
        self._inter_main = main_interrupt
        self._sock = sock
        self._sock.settimeout(1)
        self._in_buffer = ByteBuffer()
        self._out_buffer = ByteBuffer()
        self._read_thread = Thread(target=self._recv)
        self._write_thread = Thread(target=self._send)
        self._open = True
        self._read_thread.start()
        self._write_thread.start()
    def _recv(self):
        '''
        reads constantly into the buffer
        '''
        try:
            while self._open:
                try:
                    bts = self._sock.recv(4096)
                    if not len(bts):
                        raise OSError()
                    self._in_buffer.write_bytes(bts)
                except timeout:
                    pass
        except OSError:
            print("Socket closed")
            self.close()
    def _send(self):
        '''
        writes the out buffer to the socket
        '''
        try:
            while self._open:
                bts = self._out_buffer.flush()
                while len(bts):
                    sent = self._sock.send(bts)
                    bts = bts[sent:]
        except OSError:
            self._close()
    def get_buffers(self):
        return self._in_buffer, self._out_buffer
    def close(self):
        if self._open:
            self._open = False
            self._out_buffer.close()
            try:
                self._sock.shutdown(SHUT_WR)
                self._sock.close()
            except OSError:
                pass
            print("Closing input buffer")
            self._in_buffer.close()
            print("Closed input buffer")
            try:
                self._read_thread.join()
                self._write_thread.join()
            except RuntimeError:
                if self._inter_main:
                    print("Interrupting main thread")
                    kill(getpid(), SIGINT)