def __init__(self, data): self.data = data self.index = 0 self.charset = DEFAULT_CHARSET self.encoding = charset_by_name(self.charset).encoding self.use_unicode = None self.decoders = decoders
def _request_authentication(self): self.client_flag |= CAPABILITIES if self.server_version.startswith('5'): self.client_flag |= MULTI_RESULTS if self.user is None: raise ValueError("Did not specify a username") charset_id = charset_by_name(self.charset).id if isinstance(self.user, text_type): self.user = self.user.encode(self.encoding) data_init = struct.pack('<i', self.client_flag) + struct.pack("<I", 1) + \ int2byte(charset_id) + int2byte(0)*23 next_packet = 1 if self.ssl: data = pack_int24( len(data_init)) + int2byte(next_packet) + data_init next_packet += 1 if DEBUG: dump_packet(data) self._write_bytes(data) self.socket = ssl.wrap_socket(self.socket, keyfile=self.key, certfile=self.cert, ssl_version=ssl.PROTOCOL_TLSv1, cert_reqs=ssl.CERT_REQUIRED, ca_certs=self.ca) self._rfile = _makefile(self.socket, 'rb') data = data_init + self.user + b'\0' + \ _scramble(self.password.encode('latin1'), self.salt) if self.db: if isinstance(self.db, text_type): self.db = self.db.encode(self.encoding) data += self.db + int2byte(0) data = pack_int24(len(data)) + int2byte(next_packet) + data next_packet += 2 if DEBUG: dump_packet(data) self._write_bytes(data) auth_packet = self._read_packet() # if old_passwords is enabled the packet will be 1 byte long and # have the octet 254 if auth_packet.is_eof_packet(): # send legacy handshake data = _scramble_323(self.password.encode('latin1'), self.salt) + b'\0' data = pack_int24(len(data)) + int2byte(next_packet) + data self._write_bytes(data) auth_packet = self._read_packet()
def set_charset(self, charset): # Make sure charset is supported. encoding = charset_by_name(charset).encoding self._execute_command(COM_QUERY, "SET NAMES %s" % self.escape(charset)) self._read_packet() self.charset = charset self.encoding = encoding
def set_charset(self, charset): # Make sure charset is supported. encoding = charset_by_name(charset).encoding self._execute_command(COM_QUERY, "SET NAMES %s" % self.escape(charset)) yield self._read_packet() self.charset = charset self.encoding = encoding
def __init__(self, host="localhost", user=None, password="", database=None, port=3306, unix_socket=None, charset='', sql_mode=None, read_default_file=None, conv=decoders, use_unicode=None, client_flag=0, cursorclass=Cursor, init_command=None, connect_timeout=None, ssl=None, read_default_group=None, compress=None, named_pipe=None, no_delay=False, autocommit=False, db=None, passwd=None): """ Establish a connection to the MySQL database. Accepts several arguments: host: Host where the database server is located user: Username to log in as password: Password to use. database: Database to use, None to not use a particular one. port: MySQL port to use, default is usually OK. unix_socket: Optionally, you can use a unix socket rather than TCP/IP. charset: Charset you want to use. sql_mode: Default SQL_MODE to use. read_default_file: Specifies my.cnf file to read these parameters from under the [client] section. conv: Decoders dictionary to use instead of the default one. This is used to provide custom marshalling of types. See converters. use_unicode: Whether or not to default to unicode strings. This option defaults to true for Py3k. client_flag: Custom flags to send to MySQL. Find potential values in constants.CLIENT. cursorclass: Custom cursor class to use. init_command: Initial SQL statement to run when connection is established. connect_timeout: Timeout before throwing an exception when connecting. ssl: A dict of arguments similar to mysql_ssl_set()'s parameters. For now the capath and cipher arguments are not supported. read_default_group: Group to read from in the configuration file. compress; Not supported named_pipe: Not supported no_delay: Disable Nagle's algorithm on the socket autocommit: Autocommit mode. None means use server default. (default: False) db: Alias for database. (for compatibility to MySQLdb) passwd: Alias for password. (for compatibility to MySQLdb) """ if use_unicode is None and sys.version_info[0] > 2: use_unicode = True if db is not None and database is None: database = db if passwd is not None and not password: password = passwd if compress or named_pipe: raise NotImplementedError( "compress and named_pipe arguments are not supported") if ssl and ('capath' in ssl or 'cipher' in ssl): raise NotImplementedError( 'ssl options capath and cipher are not supported') self.ssl = False if ssl: if not SSL_ENABLED: raise NotImplementedError("ssl module not found") self.ssl = True client_flag |= SSL for k in ('key', 'cert', 'ca'): v = None if k in ssl: v = ssl[k] setattr(self, k, v) if read_default_group and not read_default_file: if sys.platform.startswith("win"): read_default_file = "c:\\my.ini" else: read_default_file = "/etc/my.cnf" if read_default_file: if not read_default_group: read_default_group = "client" cfg = configparser.RawConfigParser() cfg.read(os.path.expanduser(read_default_file)) def _config(key, default): try: return cfg.get(read_default_group, key) except Exception: return default user = _config("user", user) password = _config("password", password) host = _config("host", host) database = _config("database", database) unix_socket = _config("socket", unix_socket) port = int(_config("port", port)) charset = _config("default-character-set", charset) self.host = host self.port = port self.user = user or DEFAULT_USER self.password = password or "" self.db = database self.no_delay = no_delay self.unix_socket = unix_socket if charset: self.charset = charset self.use_unicode = True else: self.charset = DEFAULT_CHARSET self.use_unicode = False if use_unicode is not None: self.use_unicode = use_unicode self.encoding = charset_by_name(self.charset).encoding client_flag |= CAPABILITIES client_flag |= MULTI_STATEMENTS if self.db: client_flag |= CONNECT_WITH_DB self.client_flag = client_flag self.cursorclass = cursorclass self.connect_timeout = connect_timeout self._result = None self._affected_rows = 0 self.host_info = "Not connected" #: specified autocommit mode. None means use server default. self.autocommit_mode = autocommit self.encoders = encoders # Need for MySQLdb compatibility. self.decoders = conv self.sql_mode = sql_mode self.init_command = init_command self._connect()
def __init__( self, host="localhost", user=None, passwd="", database=None, port=3306, unix_socket=None, charset="", sql_mode=None, read_default_file=None, conv=decoders, use_unicode=None, client_flag=0, cursorclass=Cursor, init_command=None, connect_timeout=None, ssl=None, read_default_group=None, compress=None, named_pipe=None, no_delay=False, autocommit=False, db=None, io_loop=None, ): """ Establish a connection to the MySQL database. Accepts several arguments: host: Host where the database server is located user: Username to log in as passwd: Password to use. database: Database to use, None to not use a particular one. port: MySQL port to use, default is usually OK. unix_socket: Optionally, you can use a unix socket rather than TCP/IP. charset: Charset you want to use. sql_mode: Default SQL_MODE to use. read_default_file: Specifies my.cnf file to read these parameters from under the [client] section. conv: Decoders dictionary to use instead of the default one. This is used to provide custom marshalling of types. See converters. use_unicode: Whether or not to default to unicode strings. This option defaults to true for Py3k. client_flag: Custom flags to send to MySQL. Find potential values in constants.CLIENT. cursorclass: Custom cursor class to use. init_command: Initial SQL statement to run when connection is established. connect_timeout: Timeout before throwing an exception when connecting. ssl: A dict of arguments similar to mysql_ssl_set()'s parameters. For now the capath and cipher arguments are not supported. read_default_group: Group to read from in the configuration file. compress; Not supported named_pipe: Not supported no_delay: Disable Nagle's algorithm on the socket autocommit: Autocommit mode. None means use server default. (default: False) db: Alias for database. (for compatibility to MySQLdb) """ if use_unicode is None and sys.version_info[0] > 2: use_unicode = True if db is not None and database is None: database = db if compress or named_pipe: raise NotImplementedError("compress and named_pipe arguments are not supported") if ssl and ("capath" in ssl or "cipher" in ssl): raise NotImplementedError("ssl options capath and cipher are not supported") self.ssl = False if ssl: if not SSL_ENABLED: raise NotImplementedError("ssl module not found") self.ssl = True client_flag |= SSL for k in ("key", "cert", "ca"): v = None if k in ssl: v = ssl[k] setattr(self, k, v) if read_default_group and not read_default_file: if sys.platform.startswith("win"): read_default_file = "c:\\my.ini" else: read_default_file = "/etc/my.cnf" if read_default_file: if not read_default_group: read_default_group = "client" cfg = configparser.RawConfigParser() cfg.read(os.path.expanduser(read_default_file)) def _config(key, default): try: return cfg.get(read_default_group, key) except Exception: return default user = _config("user", user) passwd = _config("password", passwd) host = _config("host", host) database = _config("database", database) unix_socket = _config("socket", unix_socket) port = int(_config("port", port)) charset = _config("default-character-set", charset) self.host = host self.port = port self.user = user or DEFAULT_USER self.password = passwd or "" self.db = database self.no_delay = no_delay self.unix_socket = unix_socket if charset: self.charset = charset self.use_unicode = True else: self.charset = DEFAULT_CHARSET self.use_unicode = False if use_unicode is not None: self.use_unicode = use_unicode self.encoding = charset_by_name(self.charset).encoding client_flag |= CAPABILITIES client_flag |= MULTI_STATEMENTS if self.db: client_flag |= CONNECT_WITH_DB self.client_flag = client_flag self.cursorclass = cursorclass self.connect_timeout = connect_timeout self._result = None self._affected_rows = 0 self.host_info = "Not connected" #: specified autocommit mode. None means use server default. self.autocommit_mode = autocommit self.encoders = encoders # Need for MySQLdb compatibility. self.decoders = conv self.sql_mode = sql_mode self.init_command = init_command self.io_loop = io_loop or tornado.ioloop.IOLoop.current() self.stream = None
def _request_authentication(self): self.client_flag |= CAPABILITIES if self.server_version.startswith("5"): self.client_flag |= MULTI_RESULTS if self.user is None: raise ValueError("Did not specify a username") charset_id = charset_by_name(self.charset).id if isinstance(self.user, text_type): self.user = self.user.encode(self.encoding) data_init = struct.pack("<i", self.client_flag) + struct.pack("<I", 1) + int2byte(charset_id) + int2byte(0) * 23 next_packet = 1 if self.ssl: data = pack_int24(len(data_init)) + int2byte(next_packet) + data_init next_packet += 1 if DEBUG: dump_packet(data) yield self.stream.write(data) self.socket = ssl.wrap_socket( self.socket, keyfile=self.key, certfile=self.cert, ssl_version=ssl.PROTOCOL_TLSv1, cert_reqs=ssl.CERT_REQUIRED, ca_certs=self.ca, ) self._rfile = _makefile(self.socket, "rb") data = data_init + self.user + b"\0" + _scramble(self.password.encode("latin1"), self.salt) if self.db: if isinstance(self.db, text_type): self.db = self.db.encode(self.encoding) data += self.db + int2byte(0) data = pack_int24(len(data)) + int2byte(next_packet) + data next_packet += 2 if DEBUG: dump_packet(data) yield self.stream.write(data) auth_packet = MysqlPacket(self) yield auth_packet.recv_packet() auth_packet.check_error() if DEBUG: auth_packet.dump() # if old_passwords is enabled the packet will be 1 byte long and # have the octet 254 if auth_packet.is_eof_packet(): # send legacy handshake data = _scramble_323(self.password.encode("latin1"), self.salt) + b"\0" data = pack_int24(len(data)) + int2byte(next_packet) + data self.stream.write(data) auth_packet = MysqlPacket(self) yield auth_packet.recv_packet() auth_packet.check_error() if DEBUG: auth_packet.dump()
def _send_authentication(self): self.client_flag |= CAPABILITIES if self.server_version.startswith('5'): self.client_flag |= MULTI_RESULTS if self.user is None: raise ValueError, "Did not specify a username" charset_id = charset_by_name(self.charset).id self.user = self.user.encode(self.charset) data_init = struct.pack('<i', self.client_flag) + struct.pack("<I", 1) + \ int2byte(charset_id) + int2byte(0)*23 next_packet = 1 if self.ssl: data = pack_int24(len(data_init)) + int2byte(next_packet) + data_init next_packet += 1 if DEBUG: dump_packet(data) self.wfile.write(data) self.wfile.flush() self.socket = ssl.wrap_self.socketet(self.socket, keyfile=self.key, certfile=self.cert, ssl_version=ssl.PROTOCOL_TLSv1, cert_reqs=ssl.CERT_REQUIRED, ca_certs=self.ca) self.rfile = self.socket.makefile("rb") self.wfile = self.socket.makefile("wb") data = data_init + self.user+int2byte(0) + _scramble(self.password.encode(self.charset), self.salt) if self.db: self.db = self.db.encode(self.charset) data += self.db + int2byte(0) data = pack_int24(len(data)) + int2byte(next_packet) + data next_packet += 2 if DEBUG: dump_packet(data) self.wfile.write(data) self.wfile.flush() auth_packet = MysqlPacket(self) auth_packet.check_error() if DEBUG: auth_packet.dump() # if old_passwords is enabled the packet will be 1 byte long and # have the octet 254 if auth_packet.is_eof_packet(): # send legacy handshake #raise NotImplementedError, "old_passwords are not supported. Check to see if mysqld was started with --old-passwords, if old-passwords=1 in a my.cnf file, or if there are some short hashes in your mysql.user table." # TODO: is this the correct charset? data = _scramble_323(self.password.encode(self.charset), self.salt.encode(self.charset)) + int2byte(0) data = pack_int24(len(data)) + int2byte(next_packet) + data self.wfile.write(data) self.wfile.flush() auth_packet = MysqlPacket(self) auth_packet.check_error() if DEBUG: auth_packet.dump()
def _send_authentication(self): self.client_flag |= CAPABILITIES if self.server_version.startswith('5'): self.client_flag |= MULTI_RESULTS if self.user is None: raise ValueError, "Did not specify a username" charset_id = charset_by_name(self.charset).id self.user = self.user.encode(self.charset) data_init = struct.pack('<i', self.client_flag) + struct.pack("<I", 1) + \ int2byte(charset_id) + int2byte(0)*23 next_packet = 1 if self.ssl: data = pack_int24( len(data_init)) + int2byte(next_packet) + data_init next_packet += 1 if DEBUG: dump_packet(data) self.wfile.write(data) self.wfile.flush() self.socket = ssl.wrap_self.socketet( self.socket, keyfile=self.key, certfile=self.cert, ssl_version=ssl.PROTOCOL_TLSv1, cert_reqs=ssl.CERT_REQUIRED, ca_certs=self.ca) self.rfile = self.socket.makefile("rb") self.wfile = self.socket.makefile("wb") data = data_init + self.user + int2byte(0) + _scramble( self.password.encode(self.charset), self.salt) if self.db: self.db = self.db.encode(self.charset) data += self.db + int2byte(0) data = pack_int24(len(data)) + int2byte(next_packet) + data next_packet += 2 if DEBUG: dump_packet(data) self.wfile.write(data) self.wfile.flush() auth_packet = MysqlPacket(self) auth_packet.check_error() if DEBUG: auth_packet.dump() # if old_passwords is enabled the packet will be 1 byte long and # have the octet 254 if auth_packet.is_eof_packet(): # send legacy handshake #raise NotImplementedError, "old_passwords are not supported. Check to see if mysqld was started with --old-passwords, if old-passwords=1 in a my.cnf file, or if there are some short hashes in your mysql.user table." # TODO: is this the correct charset? data = _scramble_323(self.password.encode(self.charset), self.salt.encode(self.charset)) + int2byte(0) data = pack_int24(len(data)) + int2byte(next_packet) + data self.wfile.write(data) self.wfile.flush() auth_packet = MysqlPacket(self) auth_packet.check_error() if DEBUG: auth_packet.dump()