2

I'm using ftp upload function to upload files to my own ftp server , its keep through the following except ,

2015-12-10 11:35:22,985 - FtpUpload - Failed to setup connection
Exception in thread Thread-1:
Traceback (most recent call last):
  File "/usr/lib/python2.7/threading.py", line 810, in __bootstrap_inner
    self.run()
  File "/usr/lib/python2.7/threading.py", line 763, in run
    self.__target(*self.__args, **self.__kwargs)
  File "ftp_upload.py", line 122, in upload_queue
    queue.put(upload(ifn))
  File "ftp_upload.py", line 95, in upload
    upload_to_ftp(trans_dst,s3_dst)
  File "ftp_upload.py", line 53, in upload_to_ftp
    _ftp.connect(server_ip, port)
  File "/usr/lib/python2.7/ftplib.py", line 135, in connect
    self.sock = socket.create_connection((self.host, self.port), self.timeout)
  File "/usr/lib/python2.7/socket.py", line 553, in create_connection
    for res in getaddrinfo(host, port, 0, SOCK_STREAM):
gaierror: [Errno -2] Name or service not known

I've tested my ftp server with other dummy code and its working fine , here is my function ..

def upload_to_ftp(file_path, server_ip):
    try:
        #connection
        _ftp = ftplib.FTP()
        _ftp.connect(server_ip, port)
        #user and password vars must be in the config file
        _ftp.login(user=user, password=password)
        logger.info("Connecting to ftp server...")
    except:
        logger.error("Failed to setup connection")
        raise
    logger.info("Starting to upload %s to %s" % (file_path, server_ip))
    upload_file = open(file_path, 'r')
    try:
        path_split = file_path.split('/')
        file_name = path_split[len(path_split) - 1]

        _ftp.storbinary('STOR '+ file_path, upload_file, 1024)
        logger.info("File %s is uploaded." % file_path)
        upload_file.close()
    except:
        logger.error("Upload to FTP failed")
        upload_file.close()

the upload function is here :

def upload(ifn):
    """ Upload Function."""

    create_time = datetime.now().strftime("%Y/%m/%d")

    ifn_norm = resub('[ !@#$%\t"#$%&\()*\-/<=>?@^_`{|},.:]', '-', ifn).lower() + os.path.splitext(ifn)[1]

    src = "%s/%s" % (src_loc,ifn)
    dst = "%s/%s" % (dst_loc, ifn_norm)

    s3_dst = "%s/%s" % (create_time,ifn_norm)
    trans_dst = "%s/%s" % (proc_loc,ifn_norm)

    start_time = time.time()

    try:
        transcode(src,trans_dst)
    except:
        return
    trans_time = time.time() - start_time
    try:
        upload_to_ftp(trans_dst,s3_dst)
        # post request will be performed if no exception will raise in upload_to_ftp method

        # parameters for this call should be set in config file, because i don't know the url
        perform_post_req(trans_dst,s3_dst)

    except NameError as s3error:
        logger.error("Upload failed for %s , with exception %s" % (trans_dst, s3error))
        oflist.remove(ifn)
        hsize.pop(ifn)
        return

    upload_time = (time.time() - start_time) - trans_time

    logger.info("Uploading completed for %s, stats - transcode time : %.3f seconds, upload time : %.3f seconds" %(ifn,trans_time,upload_time))
    logger.info("Moving \"%s\" to \"%s\"" % (trans_dst, dst))
    rc = os.system("mv \"%s\" \"%s\"" %(trans_dst, dst))
    if rc != 0:
        logger.error("Can't move file %s - Exiting" %(trans_dst))
    else:
        logger.info("Moved file %s" %(trans_dst))
        oflist.remove(ifn)
        hsize.pop(ifn)
    return

ftp server is working fine , tested all the way ,

can you please advise

3
  • Could you update the code with actual part that call upload_to_ftp with actual values of trans_dst, s3_dst ? Commented Dec 10, 2015 at 7:58
  • updated , can you have look ? Commented Dec 10, 2015 at 8:01
  • all vars defined in config.yml Commented Dec 10, 2015 at 8:02

1 Answer 1

1

According to the function upload_to_ftp definition, it expected server ip as the second parameter.

But in the calling part, file(or directory) path is passed.

upload_to_ftp(trans_dst,s3_dst)
                        ^^^^^^

Change s3_dst with the server ip.

Sign up to request clarification or add additional context in comments.

7 Comments

thats works for me , but i face another issue , error_perm: 553 Could not create file. if i upload through script it will though this error , but if i used the same creds through ftp client it will upload normally i notice when i list dir by script its shows owner is 1001:1001 !
@Jecki, Make sure the target directory exist and path is correct.
i've tested it with ftp client all is correct even i added _ftp.dir() before start uploading to make sure im targeting the correct path ,
@Jecki, Try to replace _ftp.storbinary('STOR '+ file_path, upload_file, 1024) with _ftp.storbinary('STOR '+ file_name, upload_file, 1024)
@Jecki, If that does not work, ftp.cwd(file_path.rsplit('/', 1)[0]); _ftp.storbinary('STOR '+ file_name, upload_file, 1024)
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.