【问题标题】:get output from python script to django从 python 脚本获取输出到 django
【发布时间】:2017-11-07 12:36:32
【问题描述】:

我正在寻找从 python 脚本到我的 django 网络服务器的输出。
我将在我的脚本中使用 pySmartDL,所以即使 django 关闭自己并且 django 需要在启动时从正在运行的脚本中获取数据,我也需要它运行。

pySmartDL 示例脚本:

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()

正如您在此处看到的,脚本将在下载文件时多次打印输出:

time.sleep(0.2)

所以我需要动态获取输出。
我在 websocket(使用 redis 和 django-channels 或 django-redis)和 nodeJS 中找到了一些答案,但我找不到将脚本输出发送到 redis 服务器以及如何从 django 获取它们的代码示例。而且我对nodeJS不太了解。

感谢您的宝贵时间!

【问题讨论】:

    标签: python node.js django redis django-redis


    【解决方案1】:

    不要通过涉及 node.js 和 django 通道来使事情复杂化。这是你可以用 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)
    

    然后在你需要知道这个下载的django视图中

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

    【讨论】:

    • 所以我不需要 django-redis ?
    • 我看不出它有什么用处。它是 memcache AFAIK 的替代品
    猜你喜欢
    • 2020-09-21
    • 2012-02-01
    • 2015-01-21
    • 1970-01-01
    • 2012-02-24
    • 2011-01-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多