ftp传输最后一直卡在最后10秒_FTP上传大文件程序卡死

本文介绍了在FTP上传文件时遇到程序卡死的问题,原因是防火墙提前关闭了空闲的控制连接,导致程序无法正常结束。解决方案是改进FTP上传代码。此外,分享了使用Python ftplib库进行文件上传和查看FTP盘文件信息的示例代码。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

在使用ftp上传文件的过程中发现程序很久不结束,起初以为是缓冲区设置过小导致传输速度变慢,但是几个小时都不见结束,通过查看ftp盘的文件发现文件上传其实已经结束,可是程序却没有结束

其实通过ftp上传文件会建立两个tcp连接,一条控制连接用来提交指令和接受回复,另一条数据连接是用来传输数据的,当指令发布完成后,控制连接会进入空闲状态,tcp不会对空闲状态的连接有时间限制,但是防火墙会提前杀掉空闲连接,并且数据连接是不感知的,所以文件传输完成的通知就不会被感知从而导致程序卡死

分享一下python的ftplib传输文件以及查看ftp盘文件信息的代码

# 上传文件

import ftplib

import sys

import datetime

today = datetime.datetime.combine(datetime.date.today(), datetime.time.min)

yesterday = today - datetime.timedelta(days=1)

f=ftplib.FTP("ip") # 实例化FTP对象

f.login("user", "password") # 登录

def ftp_upload():

file_remote="filename" # 指定上传文件名

file_local="" # 要上传的文件路径

bufsize=2048 # 设置缓冲器大小

fp=open(file_local, 'rb')

result=f.storbinary("STOR " + file_remote, fp, bufsize)

print(result)

fp.close()

ftp_upload()

f.quit()

# 查看ftp盘文件信息

from ftplib import FTP, error_perm

import os

import re

class MyFTP(FTP):

encoding = "utf-8"

def getdirs(self, dirpath=None):

"""

获取当前路径或者指定路径下的文件、目录

"""

if dirpath != None:

self.cwd(dirpath)

dir_list = []

self.dir('.', dir_list.append)

dir_name_list = [dir_detail_str.split(' ')[-1] for dir_detail_str in dir_list]

return [file for file in dir_name_list if file != "." and file !=".."]

def checkFileDir(self, dirpath):

"""

检查指定路径是目录还是文件

"""

rec = ""

try:

rec = self.cwd(dirpath) # 需要判断的元素

self.cwd("..") # 如果能通过路劲打开必为文件夹,在此返回上一级

except error_perm as fe:

rec = fe # 不能通过路劲打开必为文件,抓取其错误信息

finally:

if "Not a directory" in str(rec):

return "File"

elif "Current directory is" in str(rec):

return "Dir"

else:

return "Unknow"

def get_modify_time(self, dirpath=None):

"""

得到指定目录、文件或者当前目录、文件的修改时间

"""

if dirpath != None:

if dirpath[-1] == '/':

dir_path = os.path.split(dirpath[0: -1])[0]

current_name = os.path.split(dirpath[0: -1])[1]

else:

dir_path = os.path.split(dirpath)[0]

# .strip()是为了避免出现”/ 2095-8757“这种情况,下面匹配不到

current_name = os.path.split(dirpath)[1].strip()

self.cwd(dir_path)

else:

dirpath = self.pwd()

dir_path = os.path.split(dirpath)[0]

current_name = os.path.split(dirpath)[1]

self.cwd(dir_path)

detail_list = []

self.retrlines('MLSD', detail_list.append)

current_info = ''

for detail_info in detail_list:

# 文件和目录不一样

# 文件从字符串获取名称

if detail_info.split(';')[3].strip() == current_name:

current_info = detail_info

break

if not current_info:

for detail_info in detail_list:

# 目录从字符串获取名称

if detail_info.split(';')[2].strip() == current_name:

current_info = detail_info

modify_time = re.search(r'modify=(.*);', current_info)

return modify_time

from test import MyFTP

import os

def connect_ftp(host, user, passwd):

ftpServer = MyFTP()

ftpServer.encoding = "utf-8"

ftpServer.connect(host=host)

ftpServer.login(user=user, passwd=passwd)

return ftpServer

# 连接服务器(参数是你的ftp服务器东西)

ftpServer = connect_ftp("ip", "user", "password")

print(ftpServer.getdirs())

print(ftpServer.size(filename=""))

)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值