Forums

Send files to a remote server via SFTP

I'm trying to send a file from Pythonanywhere to a remote server via SFTP. To do this, I'm trying to use Paramiko to establish an SSH connection but am getting an error when trying to connect.

My code is below:

with paramiko.SSHClient() as ssh:
    ssh.load_system_host_keys()
    ssh.connect(hostname=sftp_url, port=sftp_port, username=sftp_username, pkey=private_key)
    sftp = ssh.open_sftp()
    sftp.put(file, file_name)

It errors out on the 3rd line (ssh.connect()) with the following message:

File "/usr/lib/python3.8/socket.py", line 918, in getaddrinfo
    for res in _socket.getaddrinfo(host, port, family, type, proto, flags):
socket.gaierror: [Errno -2] Name or service not known

Would anyone know how to resolve this error or what could be causing it?

What is sftp_url and username you are using?