Ejemplo n.º 1
0
 def _set_protocol_version(self):
     self._logger.debug('IETF Hixie 75 protocol')
     self._request.ws_version = common.VERSION_HIXIE75
     self._request.ws_stream = StreamHixie75(self._request)
Ejemplo n.º 2
0
    def run(self):
        """Run the client.

        Shake hands and then repeat sending message and receiving its echo.
        """

        self._socket = socket.socket()
        self._socket.settimeout(self._options.socket_timeout)
        try:
            self._socket.connect(
                (self._options.server_host, self._options.server_port))
            if self._options.use_tls:
                self._socket = _TLSSocket(
                    self._socket, self._options.tls_module,
                    self._options.tls_version,
                    self._options.disable_tls_compression)

            version = self._options.protocol_version

            if (version == _PROTOCOL_VERSION_HYBI08
                    or version == _PROTOCOL_VERSION_HYBI13):
                self._handshake = ClientHandshakeProcessor(
                    self._socket, self._options)
            elif version == _PROTOCOL_VERSION_HYBI00:
                self._handshake = ClientHandshakeProcessorHybi00(
                    self._socket, self._options)
            else:
                raise ValueError('Invalid --protocol-version flag: %r' %
                                 version)

            self._handshake.handshake()

            self._logger.info('Connection established')

            request = ClientRequest(self._socket)

            version_map = {
                _PROTOCOL_VERSION_HYBI08: common.VERSION_HYBI08,
                _PROTOCOL_VERSION_HYBI13: common.VERSION_HYBI13,
                _PROTOCOL_VERSION_HYBI00: common.VERSION_HYBI00
            }
            request.ws_version = version_map[version]

            if (version == _PROTOCOL_VERSION_HYBI08
                    or version == _PROTOCOL_VERSION_HYBI13):
                stream_option = StreamOptions()
                stream_option.mask_send = True
                stream_option.unmask_receive = False

                if self._options.deflate_stream:
                    stream_option.deflate_stream = True

                if self._options.deflate_frame is not False:
                    processor = self._options.deflate_frame
                    processor.setup_stream_options(stream_option)

                self._stream = Stream(request, stream_option)
            elif version == _PROTOCOL_VERSION_HYBI00:
                self._stream = StreamHixie75(request, True)

            for line in self._options.message.split(','):
                self._stream.send_message(line)
                if self._options.verbose:
                    print 'Send: %s' % line
                try:
                    received = self._stream.receive_message()

                    if self._options.verbose:
                        print 'Recv: %s' % received
                except Exception, e:
                    if self._options.verbose:
                        print 'Error: %s' % e
                    raise

            self._do_closing_handshake()
Ejemplo n.º 3
0
 def test_payload_length(self):
     for length, bytes in ((0, '\x00'), (0x7f, '\x7f'), (0x80, '\x81\x00'),
                           (0x1234, '\x80\xa4\x34')):
         test_stream = StreamHixie75(_create_request_hixie75(bytes))
         self.assertEqual(
             length, test_stream._read_payload_length_hixie75())