Skip to content

ftplib cannot connect to server which has banner / welcome message configured #123911

@kegabor

Description

@kegabor

Bug report

Bug description:

Scenario:
Setup: There is a running FTP server configured to send welcome message to user, like:
Hello, this is my FTP server, please login
Test: Connect to this FTP server and validate the login banner.
Unexpected result:

    welcome_msg = new_ftp.connect(host, port, timeout)
                 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/lib64/python3.12/ftplib.py", line 162, in connect
    self.welcome = self.getresp()
                   ^^^^^^^^^^^^^^
  File "/usr/lib64/python3.12/ftplib.py", line 255, in getresp
    raise error_proto(resp)
ftplib.error_proto: test disclaimer

Based on code https://github.com/python/cpython/blame/main/Lib/ftplib.py#L162 the welcome message is handled the same way as any FTP response, but there is no restriction I could find related to banner.
It means if it's not starting with number 1-3 then error is raised. Moreover if the 4th character is not a '-', then only 1 line is read.

My suggestion is to simply read out the buffer in connect() like:

  def connect(...)
    ...
    self.welcome = self.readwelcome()
    return self.welcome

  def readwelcome(self):
    '''Get the welcome message during connect()'''
    welcome = self.sock.recv(self.maxline).decode()
    if self.debugging:
      print('*welcome* ', self.sanitize(welcome))
    return welcome

CPython versions tested on:

3.9, 3.12

Operating systems tested on:

Linux, Windows

Metadata

Metadata

Assignees

No one assigned

    Labels

    pendingThe issue will be closed if no feedback is providedstdlibStandard Library Python modules in the Lib/ directorytype-bugAn unexpected behavior, bug, or error

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions