def _handle_eof(self, packet): """Handle a MySQL EOF packet This method handles a MySQL EOF packet. When the packet is found to be an Error packet, an error will be raised. If the packet is neither and OK or an Error packet, errors.InterfaceError will be raised. Returns a dict() """ if packet[4] == '\xfe': eof = self._protocol.parse_eof(packet) self._toggle_have_next_result(eof['status_flag']) return eof elif packet[4] == '\xff': raise errors.get_exception(packet) raise errors.InterfaceError('Expected EOF packet')
def _handle_ok(self, packet): """Handle a MySQL OK packet This method handles a MySQL OK packet. When the packet is found to be an Error packet, an error will be raised. If the packet is neither an OK or an Error packet, errors.InterfaceError will be raised. Returns a dict() """ if packet[4] == '\x00': ok = self._protocol.parse_ok(packet) self._toggle_have_next_result(ok['server_status']) return ok elif packet[4] == '\xff': raise errors.get_exception(packet) raise errors.InterfaceError('Expected OK packet')
def _handle_result(self, packet): """Handle a MySQL Result This method handles a MySQL result, for example, after sending the query command. OK and EOF packets will be handled and returned. If the packet is an Error packet, an errors.Error-exception will be raised. The dictionary returned of: - columns: column information - eof: the EOF-packet information Returns a dict() """ if not packet or len(packet) < 4: raise errors.InterfaceError('Empty response') elif packet[4] == '\x00': return self._handle_ok(packet) elif packet[4] == '\xfb': return self._handle_load_data_infile(packet[5:]) elif packet[4] == '\xfe': return self._handle_eof(packet) elif packet[4] == '\xff': raise errors.get_exception(packet) # We have a text result set column_count = self._protocol.parse_column_count(packet) if not column_count or not isinstance(column_count, int): raise errors.InterfaceError('Illegal result set.') columns = [ None, ] * column_count for i in xrange(0, column_count): columns[i] = self._protocol.parse_column(self._socket.recv()) eof = self._handle_eof(self._socket.recv()) self.unread_result = True return {'columns': columns, 'eof': eof}
def _do_auth(self, username=None, password=None, database=None, client_flags=0, charset=33, ssl_options=None): """Authenticate with the MySQL server """ if client_flags & ClientFlag.SSL and ssl_options: packet = self._protocol.make_auth_ssl(charset=charset, client_flags=client_flags) self._socket.send(packet) self._socket.switch_to_ssl(**ssl_options) packet = self._protocol.make_auth(seed=self._handshake['scramble'], username=username, password=password, database=database, charset=charset, client_flags=client_flags) self._socket.send(packet) packet = self._socket.recv() if packet[4] == '\xfe': raise errors.NotSupportedError( "Authentication with old (insecure) passwords "\ "is not supported. For more information, lookup "\ "Password Hashing in the latest MySQL manual") elif packet[4] == '\xff': raise errors.get_exception(packet) try: if (not (client_flags & ClientFlag.CONNECT_WITH_DB) and database): self.cmd_init_db(database) except: raise return True
def _do_handshake(self): """Get the handshake from the MySQL server""" packet = self._socket.recv() if packet[4] == '\xff': raise errors.get_exception(packet) try: handshake = self._protocol.parse_handshake(packet) except Exception as err: raise errors.InterfaceError('Failed parsing handshake; %s' % err) regex_ver = re.compile("^(\d{1,2})\.(\d{1,2})\.(\d{1,3})(.*)") match = regex_ver.match(handshake['server_version_original']) if not match: raise errors.InterfaceError("Failed parsing MySQL version") version = tuple([int(v) for v in match.groups()[0:3]]) if version < (4, 1): raise errors.InterfaceError( "MySQL Version '%s' is not supported." % \ handshake['server_version_original']) self._handshake = handshake self._server_version = version