Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
self._my_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
self._my_socket.setsockopt(socket.IPPROTO_TCP, socket.TCP_NODELAY, 1)
self._my_socket.setsockopt(socket.SOL_SOCKET, socket.SO_SNDBUF, send_buffer_size)
self._my_socket.setsockopt(socket.SOL_SOCKET, socket.SO_RCVBUF, receive_buffer_size)
self._my_socket.settimeout(30)
# TODO: wrap SSL
if self._store.certificate:
self._my_socket = ssl.wrap_socket(self._my_socket, certfile=self._store.certificate,
ssl_version=ssl.PROTOCOL_TLSv1_2)
try:
self._my_socket.connect((host, port))
except Exception as e:
print(e)
header = Utils.dict_to_bytes(
{"Operation": "Subscription", "DatabaseName": self._database_name, "OperationVersion": 40})
self._my_socket.sendall(header)
parser = IncrementalJsonParser(self._my_socket)
response = parser.next_object()
if response['Status'] != "Ok":
if response['Status'] == "AuthorizationFailed":
raise ConnectionRefusedError(
"Cannot access database {0} because {1}".format(self._database_name, response['Message']))
elif response['Status'] == "TcpVersionMismatch":
raise InvalidOperationException(
"Cannot access database {0} because {1}".format(self._database_name, response['Message']))
options = Utils.dict_to_bytes(self._options.to_json())
self._my_socket.sendall(options)
header = Utils.dict_to_bytes(
{"Operation": "Subscription", "DatabaseName": self._database_name, "OperationVersion": 40})
self._my_socket.sendall(header)
parser = IncrementalJsonParser(self._my_socket)
response = parser.next_object()
if response['Status'] != "Ok":
if response['Status'] == "AuthorizationFailed":
raise ConnectionRefusedError(
"Cannot access database {0} because {1}".format(self._database_name, response['Message']))
elif response['Status'] == "TcpVersionMismatch":
raise InvalidOperationException(
"Cannot access database {0} because {1}".format(self._database_name, response['Message']))
options = Utils.dict_to_bytes(self._options.to_json())
self._my_socket.sendall(options)
if self.request_executor:
self.request_executor.close()
self.request_executor = RequestsExecutor.create_for_single_node(command.requested_node.url,
self._database_name,
self._store.certificate)
return parser
def send(self, command, value):
current_command_id = 0
future = Future()
try:
with self.send_lock:
self._command_id += 1
current_command_id += self._command_id
data = Utils.dict_to_bytes({"CommandId": current_command_id, "Command": command, "Param": value})
with self._confirmations_lock:
self._confirmations[current_command_id] = future
self.client_websocket.send(data)
try:
future.result(timeout=15)
except TimeoutError:
future.cancel()
raise TimeoutError("Did not get a confirmation for command #" + current_command_id)
except Exception as e:
if getattr(sys, 'gettrace', None):
self._logger.info('The coroutine raised an exception: {!r}'.format(e))
except websocket.WebSocketConnectionClosedException:
pass