def connect(self, hostname, port=SSH_PORT, username=None, password=None, pkey=None,<EOL>key_filename=None, timeout=None, allow_agent=True, look_for_keys=True,<EOL>compress=False):
for (family, socktype, proto, canonname, sockaddr) in socket.getaddrinfo(hostname, port, socket.AF_UNSPEC, socket.SOCK_STREAM):<EOL><INDENT>if socktype == socket.SOCK_STREAM:<EOL><INDENT>af = family<EOL>addr = sockaddr<EOL>break<EOL><DEDENT><DEDENT>else:<EOL><INDENT>af, _, _, _, addr = socket.getaddrinfo(hostname, port, socket.AF_UNSPEC, socket.SOCK_STREAM)<EOL><DEDENT>sock = socket.socket(af, socket.SOCK_STREAM)<EOL>if timeout is not None:<EOL><INDENT>try:<EOL><INDENT>sock.settimeout(timeout)<EOL><DEDENT>except:<EOL><INDENT>pass<EOL><DEDENT><DEDENT>retry_on_signal(lambda: sock.connect(addr))<EOL>t = self._transport = Transport(sock)<EOL>t.use_compression(compress=compress)<EOL>if self._log_channel is not None:<EOL><INDENT>t.set_log_channel(self._log_channel)<EOL><DEDENT>t.start_client()<EOL>ResourceManager.register(self, t)<EOL>server_key = t.get_remote_server_key()<EOL>keytype = server_key.get_name()<EOL>if port == SSH_PORT:<EOL><INDENT>server_hostkey_name = hostname<EOL><DEDENT>else:<EOL><INDENT>server_hostkey_name = "<STR_LIT>" % (hostname, port)<EOL><DEDENT>our_server_key = self._system_host_keys.get(server_hostkey_name, {}).get(keytype, None)<EOL>if our_server_key is None:<EOL><INDENT>our_server_key = self._host_keys.get(server_hostkey_name, {}).get(keytype, None)<EOL><DEDENT>if our_server_key is None:<EOL><INDENT>self._policy.missing_host_key(self, server_hostkey_name, server_key)<EOL>our_server_key = server_key<EOL><DEDENT>if server_key != our_server_key:<EOL><INDENT>raise BadHostKeyException(hostname, server_key, our_server_key)<EOL><DEDENT>if username is None:<EOL><INDENT>username = getpass.getuser()<EOL><DEDENT>if key_filename is None:<EOL><INDENT>key_filenames = []<EOL><DEDENT>elif isinstance(key_filename, str):<EOL><INDENT>key_filenames = [ key_filename ]<EOL><DEDENT>else:<EOL><INDENT>key_filenames = key_filename<EOL><DEDENT>self._auth(username, password, pkey, key_filenames, allow_agent, look_for_keys)<EOL>
Connect to an SSH server and authenticate to it.  The server's host key
is checked against the system host keys (see L{load_system_host_keys})
and any local host keys (L{load_host_keys}).  If the server's hostname
is not found in either set of host keys, the missing host key policy
is used (see L{set_missing_host_key_policy}).  The default policy is
to reject the key and raise an L{SSHException}.

Authentication is attempted in the following order of priority:

    - The C{pkey} or C{key_filename} passed in (if any)
    - Any key we can find through an SSH agent
    - Any "id_rsa" or "id_dsa" key discoverable in C{~/.ssh/}
    - Plain username/password auth, if a password was given

If a private key requires a password to unlock it, and a password is
passed in, that password will be used to attempt to unlock the key.

@param hostname: the server to connect to
@type hostname: str
@param port: the server port to connect to
@type port: int
@param username: the username to authenticate as (defaults to the
    current local username)
@type username: str
@param password: a password to use for authentication or for unlocking
    a private key
@type password: str
@param pkey: an optional private key to use for authentication
@type pkey: L{PKey}
@param key_filename: the filename, or list of filenames, of optional
    private key(s) to try for authentication
@type key_filename: str or list(str)
@param timeout: an optional timeout (in seconds) for the TCP connect
@type timeout: float
@param allow_agent: set to False to disable connecting to the SSH agent
@type allow_agent: bool
@param look_for_keys: set to False to disable searching for discoverable
    private key files in C{~/.ssh/}
@type look_for_keys: bool
@param compress: set to True to turn on compression
@type compress: bool

@raise BadHostKeyException: if the server's host key could not be
    verified
@raise AuthenticationException: if authentication failed
@raise SSHException: if there was any other error connecting or
    establishing an SSH session
@raise socket.error: if a socket error occurred while connecting