如何使用信号模块或其他超时 sftp.put() ?- Python [英] How to timeout sftp.put() with signal Module or other ? - Python
问题描述
我想让函数 sftp.put() 超时,我已经尝试过使用信号模块,但是如果上传时间超过 10 秒,脚本不会死.我用它来通过 ssh (paramiko) 传输文件.
I would like timeout the function sftp.put(), I have tried with signal Module but the script doesn't die if the upload time is over 10s. I use that to transfer files by ssh (paramiko).
[...]
def handler(signum, frame):
print 'Signal handler called with signal', signum
raise IOError("Couldn't upload the fileeeeeeeeeeee!!!!")
[...]
raspi = paramiko.SSHClient()
raspi.set_missing_host_key_policy(paramiko.AutoAddPolicy())
raspi.connect(ip , username= "", password= "" , timeout=10)
sftp = raspi.open_sftp()
[...]
signal.signal(signal.SIGALRM, handler)
signal.alarm(10)
sftp.put(source, destination , callback=None, confirm=True)
signal.alarm(0)
raspi.close()
[...]
更新 1:
如果服务器停止响应一段时间,我想中止传输.实际上,我的 python 脚本检查(循环)文件夹中的所有文件,并将其发送到此远程服务器.但是在这里的问题中,我想在服务器在传输过程中突然无法访问的情况下保留此功能(IP 服务器更改,不再有互联网,...).但是当我模拟断开连接时,脚本仍然停留在这个函数 sftp.put 上...)
I want to abort the transfer if the server stops responding for a while. Actually, my python script check (in loop) any files in a folder, and send it to this remote server. But in the problem here I want to leave this function in the case of the server become inaccessible suddenly during a transfer (ip server changing, no internet anymore,...). But when I simulate a disconnection, the script stays stuck at this function sftp.put anyway...)
更新 2:
当服务器在传输过程中离线时,put() 似乎永远被阻塞.这条线也会发生这种情况:
When the server goes offline during a transfer, put() seems to be blocked forever. This happens with this line too:
sftp.get_channel().settimeout(xx)
失去频道怎么办?
更新 3 &脚本目标
Ubuntu 18.04和 paramiko 2.6.0 版
你好,为了跟进您的评论和问题,我必须提供有关我非常丑陋的脚本的更多详细信息,对此感到抱歉:)
Hello, To follow your remarks and questions, I have to give more details about my very Ugly script, sorry about that :)
实际上,我不想手动杀死一个线程并打开一个新线程.对于我的应用程序,我希望脚本完全自主运行,如果在此过程中出现问题,它仍然可以继续运行.为此,我使用 Python 异常处理.除了远程服务器在传输过程中关闭之外,一切都按我的意愿进行:脚本在 put() 函数中保持阻塞状态,我认为是在循环中.下面,由于您的帮助,脚本总共包含 3 个函数来超时,但显然没有什么可以离开这个该死的 sftp.put() !你有什么新想法吗?
Actually, I don’t want to have to kill a thread manually and open a new one. For my application I want that the script run totally in autonomous, and if something wrong during the process, it can still go on. For that I use the Python exception handling. Everything does what I want except when the remote server going off during a transfer: The script stays blocked in the put() function, I think inside a loop. Below, the script contains in total 3 functions to timeout this thanks to your help, but apparently nothing can leave this damned sftp.put() ! Do you have some new idea ?
Import […]
[...]
def handler(signum, frame):
print 'Signal handler called with signal', signum
raise IOError("Couldn't upload the fileeeeeeeeeeee!!!!")
def check_time(size, file_size):
global start_time
if (time.time() - start_time) > 10:
raise Exception
i = 0
while i == 0:
try:
time.sleep(1) # CPU break
print ("go!")
#collect ip server
fichierIplist = open("/home/robert/Documents/iplist.txt", "r")
file_lines = fichierIplist.readlines()
fichierIplist.close()
last_line = file_lines [len (file_lines)-1]
lastKnowip = last_line
data = glob.glob("/home/robert/Documents/data/*")
items = len(data)
if items != 0:
time.sleep(60) #anyway
print("some Files!:)")
raspi = paramiko.SSHClient()
raspi.set_missing_host_key_policy(paramiko.AutoAddPolicy())
raspi.connect(lastKnowip, username= "", password= "" , timeout=10)
for source in data: #Upload file by file
filename = os.path.basename(source) #
destination = '/home/pi/Documents/pest/'+ filename #p
sftp = raspi.open_sftp()
signal.signal(signal.SIGALRM, handler)
signal.alarm(10)
sftp.get_channel().settimeout(10)
start_time = time.time()
sftp.put(source, destination, callback=check_time)
sftp.close()
signal.alarm(0)
raspi.close()
else:
print("noFile!")
except:
pass
推荐答案
在尝试了很多事情并在您的帮助和建议下,我找到了我想要的可靠解决方案.我在一个单独的线程中执行 sftp.put 并且我的脚本做我想做的.非常感谢您的帮助
After trying a lot of things and with your help and advice, I have found a reliable solution for what I wanted. I execute sftp.put in a separate Thread and my script do what I want. Many thanks for your help
现在,如果服务器在传输过程中关闭,60 秒后,我的脚本将继续使用:
Now if the server shuts down during a transfer, after 60 sec, my script goes on using:
[...]
import threading
[...]
th = threading.Thread(target=sftp.put, args=(source,destination))
th.start()
h.join(60)
[...]
这篇关于如何使用信号模块或其他超时 sftp.put() ?- Python的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!