本文整理汇总了Python中tweepy.auth.OAuthHandler.apply_auth方法的典型用法代码示例。如果您正苦于以下问题:Python OAuthHandler.apply_auth方法的具体用法?Python OAuthHandler.apply_auth怎么用?Python OAuthHandler.apply_auth使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类tweepy.auth.OAuthHandler
的用法示例。
在下文中一共展示了OAuthHandler.apply_auth方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: Stream
# 需要导入模块: from tweepy.auth import OAuthHandler [as 别名]
# 或者: from tweepy.auth.OAuthHandler import apply_auth [as 别名]
class Stream(object):
host = 'stream.twitter.com'
def __init__(self, consumer_key, consumer_key_secret, oauth_token, oauth_token_secret, listener, timeout=5.0, retry_count = None,retry_time = 10.0, snooze_time = 5.0, buffer_size=1500, headers=None):
self.auth = OAuthHandler(consumer_key, consumer_key_secret)#BasicAuthHandler(username, password)
self.auth.set_access_token(oauth_token, oauth_token_secret)
self.running = False
self.timeout = timeout
self.retry_count = retry_count
self.retry_time = retry_time
self.snooze_time = snooze_time
self.buffer_size = buffer_size
self.listener = listener
self.api = API()
self.headers = headers or {}
self.body = None
def _run(self):
# setup
#self.auth.apply_auth(None, None, self.headers, None)
self.auth.apply_auth(self.url, "POST", self.headers, self.params)
# enter loop
error_counter = 0
conn = None
exception = None
while self.running:
if self.retry_count and error_counter > self.retry_count:
# quit if error count greater than retry count
break
try:
conn = httplib.HTTPConnection(self.host)
conn.connect()
conn.sock.settimeout(self.timeout)
conn.request('POST', self.url, self.body, headers=self.headers)
resp = conn.getresponse()
if resp.status != 200:
if self.listener.on_error(resp.status) is False:
break
error_counter += 1
sleep(self.retry_time)
else:
error_counter = 0
self._read_loop(resp)
except timeout:
if self.listener.on_timeout() == False:
break
if self.running is False:
break
conn.close()
sleep(self.snooze_time)
except Exception, exception:
# any other exception is fatal, so kill loop
break
# cleanup
self.running = False
if conn:
conn.close()
if exception:
raise exception