-1

My Requirement is -

I have set of commands written in a "commands.ftp" file, to upload a "request.txt" file to FTP server. I want to call the command prompt from my python file and run the commands in the "commands.ftp" file one by one . Once all the command executed successfully I need the output in my python file. Suppose if the file is uploaded successfully or if any error occurred.

I saw many answers where it was recommended to use subprocess. but I am not getting exact answer to use this.

command.ftp

ftp
open hostname.com
usename
password    
quote site recfm=fb lrecl=750 blocksize=27750 cyl pri=120 sec=60
put "File_Name.txt"
QUIT

The above code is to upload a file to Mainframe CMS. I am able to upload the file successfully. But After uploading the file the response which we will receive how to capture that . I am not able to capture the output response.

Below is the code I am writing to to execute the command.ftp file.

from subprocess import Popen, PIPE

commands = open('local.ftp', 'r').read()

process = Popen("cmd.exe", shell=False, universal_newlines=True,
            stdin=PIPE, stdout=PIPE, stderr=PIPE)
out = process.communicate(commands)
print(out)

Here the out variable is not giving the output

  • What are these commands in commands.ftp? What sort of file is that? Can you post a sample commands.ftp? – tdelaney Jul 17 '21 at 17:59
  • I have edited the question now and added the code also. Problem is I am able to upload the txt file to CMS using the subprocess module. But I am not getting the response like uploaded successfully and upload size those things. – Gyanaranjan Nayak Jul 18 '21 at 06:49
  • Which operating system? On linux/mac you could use the pexpect module. On windows, it will be different. You likely could just pipe the file to the ftp command. – tdelaney Jul 18 '21 at 14:12
  • I am trying in WIndows. – Gyanaranjan Nayak Jul 19 '21 at 03:20
  • Looking at the [windows ftp](https://learn.microsoft.com/en-us/windows-server/administration/windows-commands/ftp) it has a command line parameter to take a command file. `subprocess.run("ftp -i -n -s:local.ftp", shell=True)`. Not sure whether that first "ftp" line is a problem or if you have to fiddle with the command syntax. – tdelaney Jul 19 '21 at 04:19

1 Answers1

0

Python has an FTP module built-in (import ftplib). That would be WAY smarter than trying to pass commands into the command-line "ftp" command. Windows, for example, doesn't include an "ftp" command in the box at all.

Tim Roberts
  • 48,973
  • 4
  • 21
  • 30
  • Thank you for the response. Actually i want upload the file to mainframe CMS. So I need to execute the command through .ftp file only. And I am able to upload the file successfully using the commands also. But the only problem is I am not getting the response after file is uploaded. How to capture the end response. I have aded my code in the question now. Thank you again. – Gyanaranjan Nayak Jul 18 '21 at 06:52