pythonnode.jsdjangoredisdjango-redis

get output from python script to django


I'm looking for get the outputs from a python script to my django web server.
I will use pySmartDL in my script, so i need it to run even when django close itself and django needs to get data from a running script when it start-up.

pySmartDL exemple script :

import time
from pySmartDL import SmartDL

url_100mb_file = ['http://ipv4.download.thinkbroadband.com/100MB.zip']
obj = SmartDL(url_100mb_file, progress_bar=False)
obj.start(blocking=False)

while not obj.isFinished():
        print("Speed: %s" % obj.get_speed(human=True))
        print("Already downloaded: %s" % obj.get_dl_size(human=True))
        print("Eta: %s" % obj.get_eta(human=True))
        print("Progress: %d%%" % (obj.get_progress()*100))
        print("Progress bar: %s" % obj.get_progress_bar())
        print("Status: %s" % obj.get_status())
        print("\n"*2+"="*50+"\n"*2)
        time.sleep(0.2)

if obj.isSuccessful():
        print("downloaded file to '%s'" % obj.get_dest())
        print("download task took %ss" % obj.get_dl_time(human=True))
        print("File hashes:")
        print(" * MD5: %s" % obj.get_data_hash('md5'))
        print(" * SHA1: %s" % obj.get_data_hash('sha1'))
        print(" * SHA256: %s" % obj.get_data_hash('sha256'))
else:
        print("There were some errors:")
        for e in obj.get_errors():
                print(str(e))

# Do something with obj.get_dest()

As you can see here the script will print output several times while a file is downloading with this :

time.sleep(0.2)

So i need to get the output dynamically.
I found some answer with websocket (with redis and django-channels or django-redis) and nodeJS but i can't find code example for sending the script output to redis server and how to get them from django. And i don't know much about nodeJS.

Thanks for your time!


Solution

  • Don't complicated matters by involving node.js and django channels. This is something that you can do with just redis.

    rdb = redis.Redis()
    
    while not obj.isFinished():
        print("Speed: %s" % obj.get_speed(human=True))
        print("Already downloaded: %s" % obj.get_dl_size(human=True))
        print("Eta: %s" % obj.get_eta(human=True))
        print("Progress: %d%%" % (obj.get_progress()*100))
        print("Progress bar: %s" % obj.get_progress_bar())
        print("Status: %s" % obj.get_status())
        print("\n"*2+"="*50+"\n"*2)
        rbd.set('download_progress',obj.get_progress_bar())
    
        time.sleep(0.2)
    

    Then in your django views that need to know about this download

    rdb = redis.Redis()
    val = rdb.get('download_progress')