我有一個從URL讀取的python代碼。如何關閉python中的urllib.urlopen連接
def submitTest(self,url):
response = None
if url is not None:
wptUrl = WebPageTestConfigUtils.getConfigValue('runTestURL')+"?f=json&url="+url+"&runs=3&video=1&web10=0&fvonly=1&mv=1&private=1&location=us_east_wptdriver:Chrome.DSL"
with closing(urllib.urlopen(wptUrl)) as response:
json.load(response)
return response["data"]["testId"]
我使用上下文庫docs https://docs.python.org/2/library/contextlib.html來關閉python連接。但是,然後在執行時出現以下錯誤。
Traceback (most recent call last):
File "webPageTestUtils.py", line 59, in <module>
main()
File "webPageTestUtils.py", line 56, in main
print webPageTestProcessor.submitTest("www.amazon.com")
File "webPageTestUtils.py", line 27, in submitTest
return response["data"]["testId"]
AttributeError: addinfourl instance has no attribute '__getitem__'
我在做什麼錯在這裏。我需要一種方式來關閉連接,如果發生了不好的事情或者我已經完成了。
可能重複關閉連接(HTTP [我要調用close()後了urllib.urlopen()?]://計算器。 COM /問題/ 1522 636/should-i-call-close-after-urllib-urlopen) – 2014-11-04 11:41:47