0

I am trying to connect to a secure FTP server created based on this link. I have not followed step 8 in the link. I am able to establish the connection and also change and print directories but am unable to create new directories. I am also unable to fetch files list.

Here is my code snippet:

import ssl
from ftplib import FTP_TLS
import sys
import os
import os.path


def connect():
    ftp = FTP_TLS()
    ftp.debugging = 2
    ftp.connect('ipaddress', 21)
    ftp.set_pasv(False)
    ftp.login('user', 'passwd')
    return ftp


ftps = connect()
destdir = "/"
try:
    resp = ftps.pwd()
    ftps.cwd(destdir)
except Exception:
    ftps.mkd(destdir)
    print(resp)
root = 'C:\\Users\\****\\****\\Logs'  # local dir


for (dir, _, files) in os.walk(root):
    newdir = destdir+dir[len(root):len(dir)].replace("\\", "/")
    print(newdir)
    try:
        ftps.cwd(newdir)
    except Exception:
        ftps.mkd(newdir)

I am using python 3.7.3 and the corresponding ftplib. I would be happy to provide any other details required.

PS: I am able to connect with Filezilla and create directories.

This is the error after running.

Output Error!

I am able to create the directories successfully once I change the dir to /logs. I am getting an error "ftplib.error_perm: 500 Illegal PORT command." whenever I send cmd like retrlines or storbinary , I get this error I have searched about this and people have asked to set it to pasv mode. When I do that, I get this error. FYI, I have enabled pasv mode in the config file I tried changing the port number to a number between pasv ports enabled in the config file (between 30000-31000). It does not connect also in this case. Error returned "ConnectionRefusedError: [WinError 10061] No connection could be made because the target machine actively refused it" I am working the first time with an FTP server or for that matter any server communications, so my problems might seem trivial. I am sorry for your trouble. Thanks.

saitej
  • 53
  • 1
  • 8
  • *"I am able to connect with Filezilla and create directories."* - unlikely that you are able to create the exact same directory when logged in with the exact same credentials with Filezila but not with this program. My guess is that you are actually creating a different directory in Filezilla. Please provide the debug output of Filezilla and your program, i.e. the exact commands and responses. I'm pretty sure one can see from this that Filezilla is operating on another directory. – Steffen Ullrich Mar 31 '20 at 12:01
  • @SteffenUllrich Filezilla debug [output](https://drive.google.com/file/d/1iHHwVqGv3yDD6oY4kML__4MmZc_JPfhC/view?usp=sharing) Code Debug [Output](https://drive.google.com/file/d/11znpenITCsaYUmVeKa1oa7XuPMDiHWAl/view?usp=sharing) – saitej Mar 31 '20 at 12:54
  • Based on the output you create the directory in FileZilla within `/logs/` while in your Python code you try `/`. Likely `/logs/` is writable while `/` is not. – Steffen Ullrich Mar 31 '20 at 15:18
  • my '/' is logs on the ftp server – saitej Mar 31 '20 at 15:33
  • my dir structure is /home/username/ftp/ This is the dir linked to FTP. the immediate folder is logs. (/home/username/ftp/logs). Also, when I do ls -la i get dr-xr-xr-x 3 nobody nogroup 4096 Mar 31 07:09 . drwxr-xr-x 5 username username 4096 Mar 31 11:12 .. drwxr-xr-x 3 username username 4096 Mar 31 12:45 logs – saitej Mar 31 '20 at 15:39
  • I was pointing out to you the difference in the behavior of two clients - one working and the other not. It does not matter how the server setup is since it is the same for both clients. But if one client worked and the other client not because it behaved differently to the first then you should fix the second client to behave like the first in order to make it working – Steffen Ullrich Mar 31 '20 at 15:45
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/210685/discussion-between-saitej-and-steffen-ullrich). – saitej Mar 31 '20 at 16:43

0 Answers0