コード例 #1
0
ファイル: test_trickle.py プロジェクト: ajdavis/trickle
    def connect(self):
        client_trickle = Trickle(
            socket.socket(socket.AF_INET),
            io_loop=self.io_loop)

        addr_info = yield self.resolver.resolve(
            'localhost', self.port, socket.AF_INET)

        sock_addr = addr_info[0][1]
        yield client_trickle.connect(sock_addr)

        # Wait for server to handle connection.
        server_stream = yield self.server.test_stream
        server_trickle = Trickle(server_stream)

        raise gen.Return((client_trickle, server_trickle))
コード例 #2
0
ファイル: test_trickle.py プロジェクト: ajdavis/trickle
    def test_http(self):
        addr_info = yield self.resolver.resolve(
            'localhost',
            self.get_http_port(),
            socket.AF_INET)

        sock_addr = addr_info[0][1]
        trick = Trickle(
            socket.socket(socket.AF_INET),
            io_loop=self.io_loop)

        yield trick.connect(sock_addr)
        yield trick.write(b'GET / HTTP/1.1\r\n\r\n')

        headers = yield trick.read_until(b'\r\n\r\n')
        match = re.search(br'Content-Length: (\d+)\r\n', headers)
        content_length = int(match.group(1))
        body = yield trick.read_bytes(content_length)
        self.assertEqual(b'hello', body)
コード例 #3
0
ファイル: download_example.py プロジェクト: ajdavis/trickle
def download():
    sock = socket.socket(socket.AF_INET)
    trick = Trickle(sock)

    addr_info = yield resolver.resolve(
        'xkcd.com',
        80,
        socket.AF_INET)

    sock_addr = addr_info[0][1]

    yield trick.connect(sock_addr)
    yield trick.write(b'GET / HTTP/1.1\r\nHost: xkcd.com\r\n\r\n')

    headers = yield trick.read_until(b'\r\n\r\n')
    match = re.search(br'Content-Length: (\d+)\r\n', headers)
    content_length = int(match.group(1))

    body = yield trick.read_bytes(content_length)
    print body
コード例 #4
0
ファイル: client.py プロジェクト: EverythingMe/snakepit
 def connect(self):
     self._socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM, 0)
     self._stream = Trickle(self._socket, io_loop=self._io_loop)
     yield self._stream.connect(self._endpoint, self._connect_timeout)
     raise gen.Return(self)
コード例 #5
0
ファイル: client.py プロジェクト: EverythingMe/snakepit
class Client(ClientTransport):
    def __init__(self, endpoint, io_loop, connect_timeout, read_timeout):
        host, port = endpoint.split(':')
        self._endpoint = host, int(port)
        self._io_loop = io_loop
        self._futures = dict()
        self._connect_timeout = connect_timeout
        self._read_timeout = read_timeout
        self._reading = False
        self._socket = None
        self._stream = None

    @gen.coroutine
    def connect(self):
        self._socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM, 0)
        self._stream = Trickle(self._socket, io_loop=self._io_loop)
        yield self._stream.connect(self._endpoint, self._connect_timeout)
        raise gen.Return(self)

    @gen.engine
    def _start_read_loop(self):
        while self._futures:
            #data = yield self._stream.read_until('\r\n', timeout=self._read_timeout)
            data = yield self._stream.read_until('\r\n')
            try:
                msg = json.loads(data)
                key = msg['key']
            except KeyError:
                log.error('Malformed response data: %s', data)
                continue
            try:
                future = self._futures[key]
            except KeyError:
                log.warn('No pending request for key: %s', key)
                continue

            if 'result' in msg:
                future.set_result(msg['result'])
            elif 'exception' in msg:
                future.set_exception(RemoteException(msg['exception']))
            else:
                logging.error('Response contains no `result` or `exception`: %s', msg)

            del self._futures[key]

        self._reading = False

    def _ensure_reading(self):
        if not self._reading:
            self._start_read_loop()
            self._reading = True

    def call(self, method, *args, **kwargs):
        key = Group.hash_key(method, *args, **kwargs)
        if key in self._futures:
            return self._futures[key]

        future = concurrent.TracebackFuture()

        msg = json.dumps({
            'key': key,
            'method': method,
            'args': list(args),
            'kwargs': kwargs
        }) + '\r\n'
        self._stream.write(msg)
        self._futures[key] = future
        self._ensure_reading()
        return future