Python:如何使用urllib2和pool.map知道哪个URL失败?

Python:如何使用urllib2和pool.map知道哪个URL失败?,python,aws-lambda,Python,Aws Lambda,我试图同时调用3个URL并记录所有错误。以下是我的示例代码: urls = ["https://example.com/gives200.php", "https://example.com/alsogives200.php", "https://example.com/gives500.php"]; try: results = pool.map(urllib2.urlopen, urls); except URLError: urllib2.urlopen("http

我试图同时调用3个URL并记录所有错误。以下是我的示例代码:

urls = ["https://example.com/gives200.php", "https://example.com/alsogives200.php", "https://example.com/gives500.php"];

try:
     results = pool.map(urllib2.urlopen, urls);
 except URLError:
     urllib2.urlopen("https://example.com/log_error/?url="+URLError.url);
我只想知道哪些URL(如果有的话)错误,让他们调用
/log\u error/
URL。但是当我有这样的代码时,我得到一个错误,说
urleror
没有定义

我的代码顶部有这些导入:

import urllib2 
from multiprocessing.dummy import Pool as ThreadPool 
这是我的全部错误响应(这是使用AWS Lambda,不管它值多少钱)

如何捕获出错的URL以便知道它们是什么

更新

我发现:
urllib.error
类是
urleror
的一部分,它只是:
urllib
,而不是
urllib2

本文档页面顶部说明:

下面是我实际得到的更详细的HTTPError对象:

错误URL本身的问题仍然存在,尽管。。。目前我没有办法确定哪个URL是一个错误

更新2

显然,
str(e.url)
就是我所需要的。我没有找到关于这方面的任何文件;这完全是我的侥幸猜测

这就是现在的工作代码:

urls = ["https://example.com/gives200.php", "https://example.com/alsogives200.php", "https://example.com/gives500.php"];

try:
     results = pool.map(urllib2.urlopen, urls);
 except Exception as e:
     urllib2.urlopen("https://example.com/log_error/?url="+str(e.url)+"&code="+str(e.code)+"&reason="+e.reason;
更新3

感谢@mfripp,我再次将此代码修改为:

def my_urlopen(url):
    try:
        return urllib2.urlopen(url)
    except URLError:
        urllib2.urlopen("https://example.com/log_error/?url="+url)
        return None

def lambda_handler(event, context):

    urls = [
        "https://example.com/gives200.php", 
        "https://example.com/alsogives200.php", 
        "https://example.com/gives500.php"
    ];

    results = pool.map(urllib2.urlopen, urls);

    return urls;

我不确定exception对象是否会提供失败URL的详细信息。如果没有,则需要使用
try
catch
包装对
urlib2.urlopen(url)
的每个调用。你可以这样做:

urls = [
    "https://example.com/gives200.php", 
    "https://example.com/alsogives200.php", 
    "https://example.com/gives500.php"
]

def my_urlopen(url):
    try:
        return urllib2.urlopen(url)
    except URLError:
        urllib2.urlopen("https://example.com/log_error/?url="+url)
        return None

results = pool.map(my_urlopen, urls)
# At this point, any failed requests will have None as their value

编辑参见上文的更新3。需要与此合并以使其完全完整

我更新了原来的帖子来解释,但这正是我需要的代码我找不到任何能让我找到
e.url
的文档,这只是我这边的一个幸运猜测。

urls = [
    "https://example.com/gives200.php", 
    "https://example.com/alsogives200.php", 
    "https://example.com/gives500.php"
];

try:
     results = pool.map(urllib2.urlopen, urls);
except Exception as e:
     urllib2.urlopen("https://example.com/log_error/?url="+str(e.url)+"&code="+str(e.code)+"&reason="+e.reason;

pool.map
遇到异常时,它会引发该异常,然后终止所有其他任务。因此,使用这段代码,您可能会发现一些URL从未尝试过。如果您想尝试每个url并记录每个产生错误的url,您将需要类似于此处其他两个答案之一的内容。知道这一点非常好,谢谢!我会修改我的“精确代码”解决方案,然后接受你的答案。
urls = [
    "https://example.com/gives200.php", 
    "https://example.com/alsogives200.php", 
    "https://example.com/gives500.php"
]

def my_urlopen(url):
    try:
        return urllib2.urlopen(url)
    except URLError:
        urllib2.urlopen("https://example.com/log_error/?url="+url)
        return None

results = pool.map(my_urlopen, urls)
# At this point, any failed requests will have None as their value
urls = [
    "https://example.com/gives200.php", 
    "https://example.com/alsogives200.php", 
    "https://example.com/gives500.php"
];

try:
     results = pool.map(urllib2.urlopen, urls);
except Exception as e:
     urllib2.urlopen("https://example.com/log_error/?url="+str(e.url)+"&code="+str(e.code)+"&reason="+e.reason;