python / python/cpython

ftplib storbinary

Open
#124,850 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

type-bug
Dominant language
Python
Stars
77.2k
Forks
35.9k
PR merge metrics
PR metrics pending

Description

Bug report

Bug description:

There's a problem with SSL and the storbinary method. I get this error:

Error uploading yourfile.mp3: [SSL: UNEXPECTED_EOF_WHILE_READING] EOF occurred in violation of protocol (_ssl.c:2685)

A YouTuber posted a fix which creates your own storbinary function and eliminates some code in the original that solves the problem. I attached the fix below. You use this function in place of the method that comes with the library.

def storbinary(self, cmd, fp, blocksize=8192, callback=None, rest=None):
    self.voidcmd('TYPE I')
    with self.transfercmd(cmd, rest) as conn:
        while 1:
            buf = fp.read(blocksize)
            if not buf: break
            conn.sendall(buf)
            if callback: callback(buf)
    return self.voidresp()
    
    ----
    ftps = FTP_TLS()
    ftps.set_pasv(True)

      ....
    for upload in uploads:
        print(upload)
        with open(upload, 'rb') as file:  # Use 'with' to ensure the file is closed properly
            remote_filename = os.path.basename(upload)  # Get the base filename for uploading
            try:
                storbinary(ftps, f'STOR {remote_filename}', file)
            #    ftps.storbinary(f'STOR {remote_filename}', file) 

                print(f'Successfully uploaded: {remote_filename}')
            except all_errors as e:
                print(f'Error uploading {remote_filename}: {e}')
    # Connect to the server
    ftps.connect(ftps_host)
    ftps.login(user=ftps_user, passwd=ftps_password)
    ftps.prot_p()  # Switch to secure data connection

    
CPython versions tested on:

3.12

Operating systems tested on:

macOS

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start at ftplib.FTP_TLS.storbinary and reproduce the SSL upload failure described for Python 3.12 on macOS, comparing it with the attached replacement function. No repository file or test is named; done means the reported upload works without replacing storbinary and the behavior has regression coverage.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
networking
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.