Avatar of Isaiah Melendez
Isaiah Melendez

asked on 

Help with Python - Error generating from FTP script

Hello, Experts,

I have a question to ask regarding a python script I have built to find a file, put in an array and SFTP that to a remote location that is running errors. The error I am getting is below.

Traceback (most recent call last):
  File "/Users/isaiah.melendez/Documents/python/alr_to_ho.py", line 38, in <module>
    ftpTransmit()
  File "/Users/isaiah.melendez/Documents/python/alr_to_ho.py", line 34, in ftpTransmit
    sftp.put(ftpALR[i], 'C:\CBASYNC')
  File "/Users/isaiah.melendez/Documents/python/venv/lib/python3.7/site-packages/pysftp/__init__.py", line 364, in put
    confirm=confirm)
  File "/Users/isaiah.melendez/Documents/python/venv/lib/python3.7/site-packages/paramiko/sftp_client.py", line 759, in put
    return self.putfo(fl, remotepath, file_size, callback, confirm)
  File "/Users/isaiah.melendez/Documents/python/venv/lib/python3.7/site-packages/paramiko/sftp_client.py", line 714, in putfo
    with self.file(remotepath, "wb") as fr:
  File "/Users/isaiah.melendez/Documents/python/venv/lib/python3.7/site-packages/paramiko/sftp_client.py", line 372, in open
    t, msg = self._request(CMD_OPEN, filename, imode, attrblock)
  File "/Users/isaiah.melendez/Documents/python/venv/lib/python3.7/site-packages/paramiko/sftp_client.py", line 813, in _request
    return self._read_response(num)
  File "/Users/isaiah.melendez/Documents/python/venv/lib/python3.7/site-packages/paramiko/sftp_client.py", line 865, in _read_response
    self._convert_status(msg)
  File "/Users/isaiah.melendez/Documents/python/venv/lib/python3.7/site-packages/paramiko/sftp_client.py", line 898, in _convert_status
    raise IOError(text)
OSError

Open in new window



Python Code:
#import libraries
import os
import pysftp
from locker import *

# query for file function
alr_file = '/Users/isaiah.melendez/Documents/python/test'
#alr_file = 'C:\TBOffice\TBOLink\TEK\Export'
#prep list to store data in
ftpALR = []

ftp_un_dec = cipher_suite.decrypt(cipher_text2)
ftp_pw_dec = cipher_suite.decrypt(cipher_text)

#look for file and store in list and iterate
def findFile():

    for item in os.listdir(alr_file):
        if item.endswith('.mdb'):
            #ftpALR.append(alr_file + '\\' + item)
            ftpALR.append(alr_file + '/' + item)
            #print(ftpALR)

# ftp transmission function
def ftpTransmit():
    # list ftp dir
    cnopts = pysftp.CnOpts()
    cnopts.hostkeys = None
    with pysftp.Connection(host='co2.cbapps.com', username=ftp_un_dec.decode('utf-8'), password=ftp_pw_dec.decode('utf-8'), port=2222, cnopts=cnopts) as sftp:

        length = len(ftpALR)

        for i in range(length):
            sftp.put(ftpALR[i], 'C:\CBASYNC')
    
    print("Transmission Success")
findFile()
ftpTransmit()

Open in new window


I have the user credentials being passed by a locker.py file and imported into this file.

Let me know your thoughts as to why this might be happening?
ProgrammingPython

Avatar of undefined
Last Comment
Isaiah Melendez
Avatar of noci
noci

When using \ as separator in path strings you need to escape them with an extra \     'C:\\CBASYNC'
(because \ is the escape carater for all kinds of special marks... \' , \", \n, \t ,  etc. etc.)

OSError means the OS has been passed invalid infomration.
Avatar of Isaiah Melendez

ASKER

@noci So, I forgot to mention, my apologies. I am developing this script on mac (unix). When I modify the code just like you mentioned I still get the same error. The remote sftp server which I have access to the user is allowed access to the remote dir for you to drop files in is windows (hence C:\\CBASYNC).

#import libraries
import os
import pysftp
from locker import *

# query for file function
alr_file = '/Users/isaiah.melendez/Documents/python/test'
#alr_file = 'C:\TBOffice\TBOLink\TEK\Export'
#prep list to store data in
ftpALR = []

ftp_un_dec = cipher_suite.decrypt(cipher_text2)
ftp_pw_dec = cipher_suite.decrypt(cipher_text)

#look for file and store in list and iterate
def findFile():

    for item in os.listdir(alr_file):
        if item.endswith('.mdb'):
            #ftpALR.append(alr_file + '\\' + item)
            ftpALR.append(alr_file + '/' + item)
            print(ftpALR)

# ftp transmission function
def ftpTransmit():
    # list ftp dir
    cnopts = pysftp.CnOpts()
    cnopts.hostkeys = None
    with pysftp.Connection(host='co2.cbapps.com', username=ftp_un_dec.decode('utf-8'), password=ftp_pw_dec.decode('utf-8'), port=2222, cnopts=cnopts) as sftp:

        length = len(ftpALR)

        for i in range(length):
            sftp.put(ftpALR[i], 'C:\\CBASYNC')
    
    print("Transmission Success")
findFile()
ftpTransmit()

Open in new window

Avatar of noci
noci

I tried the code (without cipher_suite)  and it works.

You may still need to look at the paths for sftp.put()
Avatar of Isaiah Melendez

ASKER

why would cipher suite not work? thoughts?

I think I can look at the remote path. and troubleshoot that.
Avatar of noci
noci

I thought the cipher_suite was unimportant and i couldn't find the sources for it quick enough,  so i skipped those parts and used hardcoded username & password & hostname & pathnames on my server( on linux btw).
Avatar of Isaiah Melendez

ASKER

yeah so that is part of the fernet python lib. I called it from the import from the locker py file. would you like me to share that code as well?
ASKER CERTIFIED SOLUTION
Avatar of noci
noci

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
Avatar of Isaiah Melendez

ASKER

noci you were right I forgot to put the (local, remote path + file) in the put. thanks for your help.
Programming
Programming

Programming includes both the specifics of the language you’re using, like Visual Basic, .NET, Java and others, but also the best practices in user experience and interfaces and the management of projects, version control and development. Other programming topics are related to web and cloud development and system and hardware programming.

55K
Questions
--
Followers
--
Top Experts
Get a personalized solution from industry experts
Ask the experts
Read over 600 more reviews

TRUSTED BY

IBM logoIntel logoMicrosoft logoUbisoft logoSAP logo
Qualcomm logoCitrix Systems logoWorkday logoErnst & Young logo
High performer badgeUsers love us badge
LinkedIn logoFacebook logoX logoInstagram logoTikTok logoYouTube logo