我的Raspberry Pi位于远程位置(通过WIFI加密狗连接互联网),互联网连接不是恒定的
我编写了一个python程序,将传感器读取数据上传到FTP服务器(已完成)
文件将每隔15分钟上传一次(互联网正常工作时)
我的要求
我想在internet不工作时对文件排队,在internet连接恢复时立即发送所有文件。请帮帮我。我在下面附上了我的代码
from time import sleep
import ftplib
import threading
try:
# for python3
import queue
from urllib.request import urlopen
except:
# for python2
import Queue as queue
from urllib2 import urlopen
class SensorReader():
#Initialize
def __init__(self,FTPURL,FTPUN,FTPPas):
self.FTPURL = FTPURL
self.FTPUserName = FTPUN
self.FTPPassword = FTPPas
#Data as FTPUpload using thread
def FTPThread(self, fName):
#Upload text file to FTP
FTPUp= threading.Thread(target=self.FTPUpload, args=(fName,))
FTPUp.start()
#Check if internet working
def internet_on(self):
for timeout in [1,5,10,15,60,90,120,150,180]:
try:
response=urllib2.urlopen('http://google.com',timeout=timeout)
return True
except urllib2.URLError as err: pass
return False
#Upload file to FTP
def FTPUpload(self, file):
#check if the internet
if self.internet_on:
try:
print(self.FTPURL)
ftp = ftplib.FTP(self.FTPURL)
ftp.login(self.FTPUserName, self.FTPPassword)
ftp.cwd('/')
ftp.storlines("STOR " + file, open(file, 'rb'))
except Exception as exc:
print(exc)
pass
if __name__ == "__main__":
FTPURL = ''
FTPUN = ''
FTPPas = ''
m = SensorReader(FTPURL,FTPUN,FTPPas)
fName = 'SensorRead.txt'
m.FTPThread(fName)
目前没有回答
相关问题 更多 >
编程相关推荐