在 FTP 中循环失败连接 Pyth3.6
Looping in FTP fail connection Pyth3.6
我正在使用一个 FTP 服务器,当我没有响应(如互联网故障或其他原因)时,我想尝试不断连接它。我证明在正常情况下我能够成功连接,但我现在想要的是在一定时间内循环连接,因为在尝试连接 Jupyter notebook 几秒钟后给我一个错误并停止程序。
objective 是为了能够不断地尝试连接到 ftp 服务器直到它连接然后跳转到下一个语句 While a==1: 所以因为我有那个 jupyter笔记本问题我尝试的是在 5 秒后打破循环的地方放置一个 if。
有人对此有任何其他解决方案吗它仍然没有work.Thx阅读:)
while a==0:
print('starting 1rst loop')
while True:
timeout = time.time() + 5 # 5 seconds from now
while a==0 :
print("Connecting to FTP Server")
#domain name or server ip:
ftp = FTP('ftpIP')
#Passw and User
ftp.login(user=XXX, passwd = XXX)
print('Connected to the FTP server')
ftp.quit()
ftp.close()
a= a+1
if a==0 and time.time() > timeout:
timeout=0
break
while a==1:
虽然不是很明白你的意思,但是这个看起来怎么样?
import time
import socket
from ftplib import FTP
def try_connect(host, user, passwd):
print('Connecting to FTP Server')
ftp = FTP()
try:
# domain name or server ip
ftp.connect(host, timeout=5)
except socket.error:
print('Connect failed!')
return False
# Passwd and User
ftp.login(user, passwd)
print('Connected to the FTP server')
ftp.quit()
ftp.close()
return True
def main():
while True:
try_connect('192.168.1.1', user='anonymous', passwd='')
print()
time.sleep(5)
if __name__ == '__main__':
main()
它每 5 秒尝试连接 FTP 并输出结果。
我正在使用一个 FTP 服务器,当我没有响应(如互联网故障或其他原因)时,我想尝试不断连接它。我证明在正常情况下我能够成功连接,但我现在想要的是在一定时间内循环连接,因为在尝试连接 Jupyter notebook 几秒钟后给我一个错误并停止程序。
objective 是为了能够不断地尝试连接到 ftp 服务器直到它连接然后跳转到下一个语句 While a==1: 所以因为我有那个 jupyter笔记本问题我尝试的是在 5 秒后打破循环的地方放置一个 if。
有人对此有任何其他解决方案吗它仍然没有work.Thx阅读:)
while a==0:
print('starting 1rst loop')
while True:
timeout = time.time() + 5 # 5 seconds from now
while a==0 :
print("Connecting to FTP Server")
#domain name or server ip:
ftp = FTP('ftpIP')
#Passw and User
ftp.login(user=XXX, passwd = XXX)
print('Connected to the FTP server')
ftp.quit()
ftp.close()
a= a+1
if a==0 and time.time() > timeout:
timeout=0
break
while a==1:
虽然不是很明白你的意思,但是这个看起来怎么样?
import time
import socket
from ftplib import FTP
def try_connect(host, user, passwd):
print('Connecting to FTP Server')
ftp = FTP()
try:
# domain name or server ip
ftp.connect(host, timeout=5)
except socket.error:
print('Connect failed!')
return False
# Passwd and User
ftp.login(user, passwd)
print('Connected to the FTP server')
ftp.quit()
ftp.close()
return True
def main():
while True:
try_connect('192.168.1.1', user='anonymous', passwd='')
print()
time.sleep(5)
if __name__ == '__main__':
main()
它每 5 秒尝试连接 FTP 并输出结果。