How to connect to an FTP server using digital SHA certificate

Asked

Viewed 53 times

0

I am trying to connect to an FTP server that uses a SHA certificate (.crt)

When I try to open the connection to the server I get a return 230 indicating that I am logged in to the server. Follow code below:

from ftplib import FTP_TLS

ftps = FTP_TLS()
ftps.connect(host, porta)
ftps.login(user, pwd)

But when I try to extract the information from the files belonging to that server I get the following error:

ftps.nlst()
---------------------------------------------------------------------------
OSError                                   Traceback (most recent call last)
<ipython-input-45-59055872d54f> in <module>
      4 ftps.connect(host, porta)
      5 ftps.login(user, pwd)
----> 6 ftps.nlst()

/anaconda3/lib/python3.7/ftplib.py in nlst(self, *args)
    557             cmd = cmd + (' ' + arg)
    558         files = []
--> 559         self.retrlines(cmd, files.append)
    560         return files
    561 

/anaconda3/lib/python3.7/ftplib.py in retrlines(self, cmd, callback)
    466             callback = print_line
    467         resp = self.sendcmd('TYPE A')
--> 468         with self.transfercmd(cmd) as conn, \
    469                  conn.makefile('r', encoding=self.encoding) as fp:
    470             while 1:

/anaconda3/lib/python3.7/ftplib.py in transfercmd(self, cmd, rest)
    397     def transfercmd(self, cmd, rest=None):
    398         """Like ntransfercmd() but returns only the socket."""
--> 399         return self.ntransfercmd(cmd, rest)[0]
    400 
    401     def login(self, user = '', passwd = '', acct = ''):

/anaconda3/lib/python3.7/ftplib.py in ntransfercmd(self, cmd, rest)
    796 
    797         def ntransfercmd(self, cmd, rest=None):
--> 798             conn, size = FTP.ntransfercmd(self, cmd, rest)
    799             if self._prot_p:
    800                 conn = self.context.wrap_socket(conn,

/anaconda3/lib/python3.7/ftplib.py in ntransfercmd(self, cmd, rest)
    359             host, port = self.makepasv()
    360             conn = socket.create_connection((host, port), self.timeout,
--> 361                                             source_address=self.source_address)
    362             try:
    363                 if rest is not None:

/anaconda3/lib/python3.7/socket.py in create_connection(address, timeout, source_address)
    725 
    726     if err is not None:
--> 727         raise err
    728     else:
    729         raise error("getaddrinfo returns an empty list")

/anaconda3/lib/python3.7/socket.py in create_connection(address, timeout, source_address)
    714             if source_address:
    715                 sock.bind(source_address)
--> 716             sock.connect(sa)
    717             # Break explicitly a reference cycle
    718             err = None

OSError: [Errno 51] Network is unreachable

Any idea how to execute the command correctly? Or apply the SHA certificate in any way?

  • 1

    the mistake [Errno 51] Network is unreachable states that the server was not found. The certificate error would be in ssl.py and not in the socket.py

  • Any idea how to connect to the server using a . crt certificate?

  • The documentation speaks to pass the certificate as a parameter in FTP_TLS. Behold: ftplib.FTP_TLS(host='', user='', passwd='', acct='', keyfile=None, certfile=None, context=None, timeout=None, source_address=None, *, encoding='utf-8'). By the documentation the parameter is context. See these two links: ftplib and ssl. In the second you will see how to load the .crt.

  • I tried to create a context using the following code: context.load_cert_chain(certfile = "./certificate.crt") However I got the error: [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed: My certificate has the RSA + Certificate key, so I searched I need to use a socket_wraper() in this case, but I have no idea how to do it.

  • I don’t do certificates, but it looks like it’s that you seek.

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.