当前位置 : 主页 > 网页制作 > HTTP/TCP >

协程gevent下载代码

来源:互联网 收集:自由互联 发布时间:2021-06-16
import urllib.request import gevent from gevent import monkey# 打补丁monkey.patch_all() def downloader(img_name, img_url): req = urllib.request.urlopen(img_url) img_content = req.read() with open(img_name, " wb " ) as f: f.write(img_cont
import urllib.request
import gevent
from gevent import monkey
# 打补丁
monkey.patch_all()


def downloader(img_name, img_url):
    req = urllib.request.urlopen(img_url)

    img_content = req.read()

    with open(img_name, "wb") as f:
        f.write(img_content)

def main():
    gevent.joinall([
            gevent.spawn(downloader, "h2.html", "https://hnfile.artlets.cnhttp://img.558idc.com/uploadfile/tkTicket/12/0214003019238-dcd1bd93.html"),
    ])


if __name__ == __main__:
    main()
网友评论