def connect(self, host="localhost", port=3306, user="", password="", db="", autocommit=None, charset=None, use_unicode=False): """connects to the given host and port with user and password""" #self.log.debug("connect mysql client %s %s %s %s %s", id(self), host, port, user, password) try: #parse addresses of form str <host:port> assert type(host) == str, "make sure host is a string" if host[0] == '/': #assume unix domain socket addr = host elif ':' in host: host, port = host.split(':') port = int(port) addr = (host, port) else: addr = (host, port) assert self.state == self.STATE_INIT, "make sure connection is not already connected or closed" self.state = self.STATE_CONNECTING self.socket = socket.create_connection(addr) self.reader = BufferedPacketReader(self.socket, self.buffer) self.writer = BufferedPacketWriter(self.socket, self.buffer) self._handshake(user, password, db, charset) #handshake complete client can now send commands self.state = self.STATE_CONNECTED if autocommit == False: self.set_autocommit(False) elif autocommit == True: self.set_autocommit(True) else: pass #whatever is the default of the db (ON in the case of mysql) if charset is not None: self.set_charset(charset) self.set_use_unicode(use_unicode) return self except gevent.Timeout: self.state = self.STATE_INIT raise except ClientLoginError: self.state = self.STATE_INIT raise except: self.state = self.STATE_ERROR raise