def _connect(self): sock = None try: if self.unix_socket and (self.host == 'localhost' or self.host == '127.0.0.1'): sock = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM) t = sock.gettimeout() sock.settimeout(self.connect_timeout) sock.connect(self.unix_socket) sock.settimeout(t) self.host_info = "Localhost via UNIX socket" if DEBUG: print('connected using unix_socket') else: sock = socket.create_connection((self.host, self.port), self.connect_timeout) self.host_info = "socket %s:%d" % (self.host, self.port) if DEBUG: print('connected using socket') except socket.error as e: if sock: sock.close() raise OperationalError( 2003, "Can't connect to MySQL server on %r (%s)" % (self.host, e.args[0])) self.socket = sock self._get_server_information() self._request_authentication()
def __recv_packet(self): """Parse the packet header and read entire packet payload into buffer.""" packet_header = self.__recv_from_socket(4) if len(packet_header) < 4: raise OperationalError( 2013, "Lost connection to MySQL server during query") bytes_to_read = unpack_uint24(packet_header) # TODO: check packet_num is correct (+1 from last packet) # self.packet_number = ord(packet_header[3:]) recv_data = self.__recv_from_socket(bytes_to_read) if len(recv_data) < bytes_to_read: raise OperationalError( 2013, "Lost connection to MySQL server during query") self.__data = recv_data
def __recv_from_socket(self, size): r = b'' while size: recv_data = self._socket.recv(size) if not recv_data: raise OperationalError(2013, "Lost connection to MySQL server during query") size -= len(recv_data) r += recv_data return r