Home > Blockchain >  python ftplib TypeError: a bytes-like object is required, not 'str'
python ftplib TypeError: a bytes-like object is required, not 'str'

Time:03-10

i'm try to use the ftp lib to upload file to server.

but have the error

TypeError: a bytes-like object is required, not 'str'

enter image description here
enter image description here

CodePudding user response:

When you are opening f on line 107, try 'rb' instead of 'r'

That may be sufficient to do the trick.

For SFTP in the past, I've used paramiko. A code snip is below:

import paramiko

def sendFile(**kwargs):

    trans = paramiko.Transport(kwargs['host'], kwargs['port'])  
    trans.connect(username = kwargs['username'], password = kwargs['password'])
    conn = paramiko.SFTPClient.from_transport(trans)
    conn.put(sourceFile, destFile)  

I've severely redacted the above to get basics across.

(From experience) I'd recommend you do the following as part of a wider solution:

  1. Copy your original source file and rename as a .partial file or similar (to prevent changes to source mid transfer)
  2. Check remote (dest) area for existence of similar file before trying to copy, delete if its older or different size, abandon transfer if same
  3. Transfer the .partial file
  4. Check that the .partial file at dest is same as source (checksums) 5a. If same, remove the .partial from destFile 5b. If not same, repeat transfer
  5. When transfer success, delete the .partial file from source area
  • Related