Web サイトをロードし、Web サイトのリソースをロードする必要がある時間をログに記録するスクリプト (ヘッドレス ブラウザ) を作成しています。
#!/usr/bin/env python
import gtk
import time
import webkit
import gobject
class Resource():
__signals = [
'content-length-received',
'load-failed',
'load-finished',
'response-received'
]
_start = time.time()
_durations = {}
def __init__(self, webkit_resource):
self._webkit_resource = webkit_resource
# literally copy the uri
self._uri = "".join([c for c in self._webkit_resource.get_uri()])
self._connect_callbacks()
@property
def uri(self):
return self._uri
def _connect_callbacks(self):
for s_id in self.__signals:
self._webkit_resource.connect(s_id, self._on_callback, s_id)
def _on_callback(self, *args):
self._durations[args[-1]] = time.time() - self._start
print "==>", self._uri, "==>", args[-1], "==>", self._durations[args[-1]]
class Browser(webkit.WebView):
def __init__(self):
webkit.WebView.__init__(self)
gobject.threads_init()
self.connect('resource-request-starting',
self._on_resource_request_starting)
def _on_resource_request_starting(self, view, frame, resource, request, response):
c_resource = Resource(resource)
if __name__ == "__main__":
browser = Browser()
browser.open("http://amazon.com/")
gtk.main()
コードは正常に実行されますが、突然、次の奇妙なトレースバックが表示されます。
==> http://amazon.com/ ==> content-length-received ==> 0.98512005806
==> http://www.amazon.com/ ==> content-length-received ==> 0.985180854797
==> http://z-ecx.images-amazon.com/images/G/01/browser-scripts/us-site-wide-css-beacon/site-wide-5659237078._V1_.css ==> content-length-received ==> 0.986252069473
==> http://z-ecx.images-amazon.com/images/G/01/browser-scripts/us-site-wide-css-beacon/site-wide-5659237078._V1_.css ==> content-length-received ==> 0.987208843231
==> http://z-ecx.images-amazon.com/images/G/01/browser-scripts/us-site-wide-css-beacon/site-wide-5659237078._V1_.css ==> load-finished ==> 0.999194860458
==>
Traceback (most recent call last):
File "test.py", line 37, in _on_callback
print "==>", self._uri, "==>", args[-1], "==>", self._durations[args[-1]]
AttributeError: Resource instance has no attribute '_uri'
==>
Traceback (most recent call last):
File "test.py", line 37, in _on_callback
print "==>", self._uri, "==>", args[-1], "==>", self._durations[args[-1]]
AttributeError: Resource instance has no attribute '_uri'
==>
私はself._uriを設定しました。私は実際に各文字を1つずつコピーします。どこかで削除されていますか?