twisted.web.clientでBasic認証
どうも標準ではできないみたいなので、以下のurlのpatchを参考にして手動で書き換えてみた。
差分ファイルを置いておくが、patchとして当てるには適宜書き換える必要があると思う
[Twisted-Python] PATCH: twisted.web.client Basic/Digest Authentication
http://twistedmatrix.com/pipermail/twisted-python/2004-October/008748.html
サンプル
以下のソースではtwitterのfriends_timelineを取得している
#!/usr/bin/env python from twisted.internet import reactor from twisted.web import client import urllib2 def printPage(data): print data reactor.stop() def printError(failure): print 'Error: %s' % failure.getErrorMessage() reactor.stop() def get(callbackfunc,errfunc = printError): u = urllib2.HTTPPasswordMgr() u.add_password('Twitter API', 'http://twitter.com/', 'user', 'pass') d = client.getPage('http://twitter.com/statuses/friends_timeline.json', passwdMgr=u) #d = client.getPage('http://twitter.com/statuses/public_timeline.json') d.addCallback(callbackfunc) d.addErrback(errfunc) reactor.run() def main(): get(printPage) if __name__ == "__main__": main()
しかしこうしてみるとpythonでwebから非同期に情報を取得する、GUIアプリケーションって少ないのかなぁ。まあ今はWeb主体だろうけど。
自分の調べた感じ非同期に受信するには、
- asyncore+socketから組み上げる
- TornadeのAyncHTTPClient http://kzk9.net/blog/2009/09/twinado_is_tornado_plus_async-python-twitter.html
- twisted
くらいしか見当たらないけど。