python ftplib 错误 : Failed to setup connection

标签 python python-2.7

我正在使用 ftp 上传功能将文件上传到我自己的 ftp 服务器,它通过以下除了,

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

我已经用其他虚拟代码测试了我的 ftp 服务器,它工作正常,这是我的函数..

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()

上传功能在这里:

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 服务器工作正常,一路测试,

你能告诉我吗

最佳答案

根据函数upload_to_ftp的定义,它期望服务器ip作为第二个参数。

但在调用部分,文件(或目录)路径被传递。

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

s3_dst改成服务器ip。

关于python ftplib 错误 : Failed to setup connection,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34196455/

相关文章:

python - 使用 lxml 删除 xml 节点

python - 如何将 pandas.series.groupby() 表从计数转换为百分比频率?

python - 使用Python将图像下载到sqlite数据库

Windows 10 中的 Python PIP 路径

python - Windows 机器上使用 Python 2.7 和 3.4 的 Pip

python - 如何从两个列表(其中一个是嵌套的)创建字典?

python - 是否有 Python 的 GUI 库允许您编译为 EXE(windows)和 APP(Mac)?

python - Matplotlib 代码由于标签而无法执行?

string - 如何将 selenium webelements 转换为 python 中的字符串列表

python - `del x` 在 Python 中有用吗?