推荐学习书目
› Learn Python the Hard Way
Python Sites
› PyPI - Python Package Index
› http://diveintopython.org/toc/index.html
› Pocoo
值得关注的项目
› PyPy
› Celery
› Jinja2
› Read the Docs
› gevent
› pyenv
› virtualenv
› Stackless Python
› Beautiful Soup
› 结巴中文分词
› Green Unicorn
› Sentry
› Shovel
› Pyflakes
› pytest
Python 编程
› pep8 Checker
Styles
› PEP 8
› Google Python Style Guide
› Code Style from The Hitchhiker's Guide
madlifer
V2EX  ›  Python

Python 大佬们看下这个脚本有什么问题吗?

  •  
  •   madlifer · Nov 2, 2020 · 3087 views
    This topic created in 2155 days ago, the information mentioned may be changed or developed.

    在改一个扫国内 Cloudfront 节点的 python 脚本,遇到了个搞不懂的问题:

    核心脚本截图如下:

    img

    现在的问题是,如果数据量很少,就可以输出的 result.txt 中

    比如:

    python 1.py ip.txt 800        #1.py 是脚本文件名  ip.txt 是 ip 地址   800 是线程
    

    ip.txt 中 只有几十个 ip 地址的时候 是可以正常输出的。

    img

    但是如果数据量比较大,比如:

    python 1.py ip-new.txt 800
    

    result.txt 中就完全是空的,不会进行输出

    求 Ptyhon 大佬告知是为什么? 完整代码在 pastebin

    https://paste.ubuntu.com/p/bMhWWGdbHT/

    8 replies  •  2020-11-02 12:42:53 +08:00
    frostming
        1
    frostming  
       Nov 2, 2020   ❤️ 1
    最关键的能知道错误是什么的地方,恰恰被你忽略了
    try:
    checkServer(host)
    except:
    continue

    请问,如果全都掉进 except 里面去了,你如何知道发生了什么错误,这时 file 里面会写内容吗
    singerll
        2
    singerll  
       Nov 2, 2020 via Android
    open 方法应该用的不是 w,是 a 吧,w 好像是清空后写入。记不清了我不是专业 python
    lpts007
        3
    lpts007  
       Nov 2, 2020 via Android
    按理说第二个命令有可能会卡住,结束不了吧。

    queue. get(blocking=false)
    madlifer
        4
    madlifer  
    OP
       Nov 2, 2020
    @frostming 谢谢老哥. 这个代码是我拿来后改的, 原因找到了,最大的问题是忘了把文件关闭,导致不结束程序不写入数据。 数据量小的时候很快就跑完一次性写入了,数据量大的时候很容易写不进去
    madlifer
        5
    madlifer  
    OP
       Nov 2, 2020
    @lpts007 是的 我一直在奇怪..明明设置了超时为啥有的时候会卡住。 我不是很懂代码,改上去试试
    renmu123
        6
    renmu123  
       Nov 2, 2020
    该升级 Python3 了
    xichuhanguguan
        7
    xichuhanguguan  
       Nov 2, 2020
    write 并没有把数据写进文件,只是在缓存当中,你需要 flush 这个方法强制把缓存中的数据写入到文件中
    wangchonglie
        8
    wangchonglie  
       Nov 2, 2020
    建议使用 with 语句
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Privacy   ·   Solana   ·   2572 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 34ms · UTC 05:35 · PVG 13:35 · LAX 22:35 · JFK 01:35
    ♥ Do have faith in what you're doing.