我想同時調用3個URL並記錄任何錯誤。下面是我的示例代碼:Python:如何使用urllib2和pool.map知道哪個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_error/
URL是錯誤的。但是當我有這樣的代碼時,我收到一個錯誤,說沒有定義URLError
。
我確實有這些進口在我的代碼的頂部:
import urllib2
from multiprocessing.dummy import Pool as ThreadPool
這裏是我的全部錯誤響應(這是使用AWS LAMBDA,不管它的價值)
{
"stackTrace": [
[
"/var/task/lambda_function.py",
27,
"lambda_handler",
"except Error as e:"
]
],
"errorType": "NameError",
"errorMessage": "global name 'URLError' is not defined"
}
怎麼辦我捕獲了錯誤的URL,所以我知道它們是什麼?
UPDATE
我想通了:在urllib.error
類URLError
是其中一部分就是:urllib
,不urllib2
。
本文檔頁面的頂部解釋說:https://docs.python.org/2/library/urllib2.html
這裏是更詳細的HTTPError對象,其實我得到: https://docs.python.org/2/library/urllib2.html#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 informing me about the dangers of pool.map
我已經修改了該代碼一次以這樣的:
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;
這是如何與pool.map協同工作的? – Bing