So pardon my ignorance. I have a script with us and it works great....until it crashes the entire app. Hahaha.
from pyftpdlib.authorizers import DummyAuthorizer
from pyftpdlib.handlers import FTPHandler
from pyftpdlib.servers import FTPServer
import threading
import os
import time
import ui
port = 2121
def getLocalIP():
import socket
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
try:
s.connect(('google.com', 80))
ip = s.getsockname()[0]
s.close()
except:
ip = 'N/A'
return ip
def imageSetter(valid_extensions=('jpg','jpeg','png')):
while True:
currentImage = ''
set = False
dirpath = os.path.expanduser('~/Documents/tetherShootPhotos')
try:
valid_files = [os.path.join(dirpath, filename) for filename in os.listdir(dirpath)]
valid_files = [f for f in valid_files if '.' in f and f.rsplit('.',1)[-1] in valid_extensions and os.path.isfile(f)]
if not valid_files:
raise ValueError("No valid images in %s" % dirpath)
imageLink = max(valid_files, key=os.path.getmtime)
if imageLink != currentImage:
newImage = ui.Image.named(imageLink)
currentImage = imageLink
v['image'].image = newImage
v['ipAddress'].hidden = True
v['connectAt'].hidden = True
set = False
except:
if set == False:
set = True
newImage = ui.Image.named('tetherShoot.png')
v['image'].image = newImage
v['ipAddress'].hidden = False
v['connectAt'].hidden = False
ipAddr = getLocalIP()+':'+str(port)
v['ipAddress'].text = ipAddr
time.sleep(1)
def main():
authorizer = DummyAuthorizer()
authorizer.add_anonymous(os.path.expanduser('~/Documents/tetherShootPhotos'), perm='elradfmwM')
handler = FTPHandler
handler.authorizer = authorizer
server = FTPServer(('0.0.0.0', port), handler)
t = threading.Thread(target=server.serve_forever)
t.start()
try:
setup = False
while True: pass
except KeyboardInterrupt:
server.close_all()
if __name__ == '__main__':
import ui
v = ui.load_view()
v['ipAddress'].hidden = True
v['connectAt'].hidden = True
v['image'].content_mode = ui.CONTENT_SCALE_ASPECT_FIT
imageUpdater = threading.Thread(target=imageSetter)
imageUpdater.start()
v.present('fullscreen',hide_title_bar=True)
main()