如何忽略和跳过 Python sftp 错误
how to ignore and skip a Python sftp error
我有这个 Python 代码,它创建桌面屏幕截图 (png) 并将其保存在本地,然后通过 sftp 连接到远程服务器并远程上传屏幕截图文件。
使用 sftp 。它运行良好,但是如果远程 sftp 服务器关闭
脚本以错误结尾。如果远程服务器 sftp 关闭,如何忽略和跳过错误
,我想避免脚本停止,而是在本地保存文件。
import mss
import mss.tools
import datetime
import time
import pysftp as sftp
count = 0
while count < 100000000:
with mss.mss() as sct:
monitor = sct.monitors[1]
timestr = time.strftime("%Y%m%d-%H%M%S")
sct.compression_level = -1
output = "d:/screen/work/" + (timestr) + ".png".format(**monitor)
sct_img = sct.grab(monitor)
mss.tools.to_png(sct_img.rgb, sct_img.size, output=output)
print(output)
s = sftp.Connection(host='127.0.0.1', username='admin', password='********')
local_path = "" +(output)+ ""
remote_path = ""
s.put(local_path, remote_path)
s.close()
time.sleep(10)
count += 1
我试过了
import mss
import mss.tools
import datetime
import time
import pysftp as sftp
count = 0
while count < 100000000:
with mss.mss() as sct:
monitor = sct.monitors[1]
timestr = time.strftime("%Y%m%d-%H%M%S")
sct.compression_level = -1
output = "d:/screen/work/" + (timestr) + ".png".format(**monitor)
sct_img = sct.grab(monitor)
mss.tools.to_png(sct_img.rgb, sct_img.size, output=output)
print(output)
try:
s = sftp.Connection(host='127.0.0.1', username='admin', password='********')
except:
print("Couldn't connect to ftp")
return False
local_path = "" +(output)+ ""
remote_path = ""
s.put(local_path, remote_path)
s.close()
time.sleep(10)
count += 1
但是 returns 这个错误
s = sftp.Connection(host='127.0.0.1', username='admin', password='******')
^
IndentationError: expected an indented block
像这样将连接包装在 try ... except
块中将使您能够在不终止进程的情况下处理错误:
# first part of your code here
try:
s = sftp.Connection(host='127.0.0.1', username='admin', password='********')
local_path = "" +(output)+ ""
remote_path = ""
s.put(local_path, remote_path)
s.close()
except Exception as e:
# do something with e here, e.g. print a log statement
# your code for saving a local copy goes here as well
# rest of your code goes here
请注意,您应该将我的通用 Exception
替换为您遇到的连接错误,以免忽略连接和传输数据时可能出现的任何其他错误,因为其他错误可能无法轻易忽略
您可以使用 try....except
块来捕获所有异常,如下所示:
import mss
import mss.tools
import datetime
import time
import pysftp as sftp
count = 0
while count < 100000000:
with mss.mss() as sct:
monitor = sct.monitors[1]
timestr = time.strftime("%Y%m%d-%H%M%S")
sct.compression_level = -1
output = "d:/screen/work/" + (timestr) + ".png".format(**monitor)
sct_img = sct.grab(monitor)
mss.tools.to_png(sct_img.rgb, sct_img.size, output=output)
print(output)
try:
s = sftp.Connection(host="127.0.0.1", username="admin", password="********")
local_path = "" + (output) + ""
remote_path = ""
s.put(local_path, remote_path)
s.close()
except:
print("Couldn't connect to ftp")
return False
time.sleep(10)
count += 1
但是不建议盲目捕获所有异常。您应该检查出现错误时会引发哪些异常,并像这样专门使用它们:
try:
"...your code block that might raise en error..."
except ZeroDivisionError:
print("Zero division error occurred")
except ValueError:
print("Value error occured")
但是,如果您无法预测可能会弹出什么错误并且不得不盲目地捕获异常,那么至少要做到 logging
。您可以查看有关捕获和记录异常的更多信息 here.
我有这个 Python 代码,它创建桌面屏幕截图 (png) 并将其保存在本地,然后通过 sftp 连接到远程服务器并远程上传屏幕截图文件。 使用 sftp 。它运行良好,但是如果远程 sftp 服务器关闭 脚本以错误结尾。如果远程服务器 sftp 关闭,如何忽略和跳过错误 ,我想避免脚本停止,而是在本地保存文件。
import mss
import mss.tools
import datetime
import time
import pysftp as sftp
count = 0
while count < 100000000:
with mss.mss() as sct:
monitor = sct.monitors[1]
timestr = time.strftime("%Y%m%d-%H%M%S")
sct.compression_level = -1
output = "d:/screen/work/" + (timestr) + ".png".format(**monitor)
sct_img = sct.grab(monitor)
mss.tools.to_png(sct_img.rgb, sct_img.size, output=output)
print(output)
s = sftp.Connection(host='127.0.0.1', username='admin', password='********')
local_path = "" +(output)+ ""
remote_path = ""
s.put(local_path, remote_path)
s.close()
time.sleep(10)
count += 1
我试过了
import mss
import mss.tools
import datetime
import time
import pysftp as sftp
count = 0
while count < 100000000:
with mss.mss() as sct:
monitor = sct.monitors[1]
timestr = time.strftime("%Y%m%d-%H%M%S")
sct.compression_level = -1
output = "d:/screen/work/" + (timestr) + ".png".format(**monitor)
sct_img = sct.grab(monitor)
mss.tools.to_png(sct_img.rgb, sct_img.size, output=output)
print(output)
try:
s = sftp.Connection(host='127.0.0.1', username='admin', password='********')
except:
print("Couldn't connect to ftp")
return False
local_path = "" +(output)+ ""
remote_path = ""
s.put(local_path, remote_path)
s.close()
time.sleep(10)
count += 1
但是 returns 这个错误
s = sftp.Connection(host='127.0.0.1', username='admin', password='******')
^
IndentationError: expected an indented block
像这样将连接包装在 try ... except
块中将使您能够在不终止进程的情况下处理错误:
# first part of your code here
try:
s = sftp.Connection(host='127.0.0.1', username='admin', password='********')
local_path = "" +(output)+ ""
remote_path = ""
s.put(local_path, remote_path)
s.close()
except Exception as e:
# do something with e here, e.g. print a log statement
# your code for saving a local copy goes here as well
# rest of your code goes here
请注意,您应该将我的通用 Exception
替换为您遇到的连接错误,以免忽略连接和传输数据时可能出现的任何其他错误,因为其他错误可能无法轻易忽略
您可以使用 try....except
块来捕获所有异常,如下所示:
import mss
import mss.tools
import datetime
import time
import pysftp as sftp
count = 0
while count < 100000000:
with mss.mss() as sct:
monitor = sct.monitors[1]
timestr = time.strftime("%Y%m%d-%H%M%S")
sct.compression_level = -1
output = "d:/screen/work/" + (timestr) + ".png".format(**monitor)
sct_img = sct.grab(monitor)
mss.tools.to_png(sct_img.rgb, sct_img.size, output=output)
print(output)
try:
s = sftp.Connection(host="127.0.0.1", username="admin", password="********")
local_path = "" + (output) + ""
remote_path = ""
s.put(local_path, remote_path)
s.close()
except:
print("Couldn't connect to ftp")
return False
time.sleep(10)
count += 1
但是不建议盲目捕获所有异常。您应该检查出现错误时会引发哪些异常,并像这样专门使用它们:
try:
"...your code block that might raise en error..."
except ZeroDivisionError:
print("Zero division error occurred")
except ValueError:
print("Value error occured")
但是,如果您无法预测可能会弹出什么错误并且不得不盲目地捕获异常,那么至少要做到 logging
。您可以查看有关捕获和记录异常的更多信息 here.