图片下载问题(python) [英] image download problem (python)

查看:83
本文介绍了图片下载问题(python)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正尝试使用多线程下载图像,该线程在python中的max_count有限.

I was trying to download images with multi-thread, which has a limited max_count in python.

每次启动download_thread时,我都将其保留,然后激活另一个.我希望下载过程可以在5s内结束,这意味着如果打开网址的费用超过5s,则下载将失败.

Each time a download_thread is started, I leave it alone and activate another one. I hope the download process could be ended in 5s, which means downloading is failed if opening the url costs more than 5s.

但是我怎么知道它并停止失败的线程???

But how can I know it and stop the failed thread???

推荐答案

您能告诉您正在使用哪个版本的python吗? 也许您也可以发布一个摘要. 从Python 2.6开始,您在urllib2.urlopen中添加了超时. 希望这会帮助你.来自python文档.

Can you tell which version of python you are using? Maybe you could have posted a snippet too. From Python 2.6, you have a timeout added in urllib2.urlopen. Hope this will help you. It's from the python docs.

urllib2.urlopen(url [,data] [, 超时]),打开URL网址, 可以是字符串或请求 对象.

urllib2.urlopen(url[, data][, timeout]) Open the URL url, which can be either a string or a Request object.

警告HTTPS请求不执行任何操作 验证服务器的 证书.数据可能是字符串 指定要发送到的其他数据 服务器,如果没有此类数据,则返回None 需要.目前,HTTP请求是 仅有的使用数据的人; HTTP 请求将是POST而不是 数据参数为时的GET 假如.数据应该在 标准 应用程序/x-www-form-urlencoded 格式. urllib.urlencode() 函数采用映射或序列 2元组,并在其中返回一个字符串 这种格式. urllib2模块发送 HTTP/1.1请求 连接:包括关闭头.

Warning HTTPS requests do not do any verification of the server’s certificate. data may be a string specifying additional data to send to the server, or None if no such data is needed. Currently HTTP requests are the only ones that use data; the HTTP request will be a POST instead of a GET when the data parameter is provided. data should be a buffer in the standard application/x-www-form-urlencoded format. The urllib.urlencode() function takes a mapping or sequence of 2-tuples and returns a string in this format. urllib2 module sends HTTP/1.1 requests with Connection:close header included.

可选的超时参数 指定以秒为单位的超时 阻止像 连接尝试(如果未指定, 全局默认超时设置 将会被使用).这实际上只是 适用于HTTP,HTTPS和FTP 连接.

The optional timeout parameter specifies a timeout in seconds for blocking operations like the connection attempt (if not specified, the global default timeout setting will be used). This actually only works for HTTP, HTTPS and FTP connections.

此函数返回类似文件的文件 对象有两种其他方法:

This function returns a file-like object with two additional methods:

geturl()-返回网址 检索到的资源,通常用于 确定是否遵循重定向 info()—返回元信息 页面中的页眉(例如标题) mimetools.Message实例的形式 (请参阅HTTP标头快速参考) 在出现错误时引发URLError.

geturl() — return the URL of the resource retrieved, commonly used to determine if a redirect was followed info() — return the meta-information of the page, such as headers, in the form of an mimetools.Message instance (see Quick Reference to HTTP Headers) Raises URLError on errors.

请注意,如果没有,则不返回任何内容 处理程序处理请求(尽管 默认安装的全局 OpenerDirector使用UnknownHandler来 确保这种情况永远不会发生.

Note that None may be returned if no handler handles the request (though the default installed global OpenerDirector uses UnknownHandler to ensure this never happens).

此外,默认安装 ProxyHandler确保请求 在以下情况下通过代理处理 他们被设置.

In addition, default installed ProxyHandler makes sure the requests are handled through the proxy when they are set.

在2.6版中进行了更改:超时为 添加.

Changed in version 2.6: timeout was added.

这篇关于图片下载问题(python)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆