具有精确流控制的http流
tidehunter的Python项目详细描述
潮架
具有精确流控制的http流
注意:自1.x起不向后兼容0.x。
亮点
安装
$ pip install tidehunter $ pip install tidehunter --upgrade
使用量
示例1(带限制):
注意:当没有提供外部Queue或StateCounter时, Hunter使用python标准Queue和内置 SimpleStateCounter 工艺设计等简单案例。
fromtidehunterimportHunter# The Hunter!h=Hunter(url='https://httpbin.org/stream/20')# Start streamingh.tide_on(limit=5)# Consume the data which should be in the data queue nowwhileh.q.qsize():print(h.q.get())# profit x 5# You can re-use the same Hunter object, with a difference limitr=h.tide_on(limit=1)# this time we only want one recordasserth.q.qsize()==1# or else there's a bug, create an issue!print(h.q.get())# more profit# r is actually just a requests.Response objectprint(r.headers)print(r.status_code)# ... read up on requests library for more information
示例2(无限制):
note:此示例使用techies,因此需要redis 安装。
假设有一个进程正在运行以下代码:
fromtechiesimportStateCounter,QueuefromtidehunterimportHunter# The data queueq=Queue(key='demo_q',host='localhost',port=6379,db=0)# The state machine and record counter (state counter)sc=StateCounter(key='demo_sc',host='localhost',port=6379,db=0)# The Hunter!h=Hunter(url='SOME_ENDLESS_STREAM_LIKE_TWITTER_FIREHOSE',q=q,sc=sc)# Start streaming, FOREVAh.tide_on()
然后将流控制和数据消耗委托给另一个/多个 其他过程,如:
fromtechiesimportStateCounter,Queue# The key is to have the SAME state countersc=StateCounter(key='demo_sc',host='localhost',port=6379,db=0)# And the SAME data queueq=Queue(key='demo_q',host='localhost',port=6379,db=0)whilesc.started:data=q.get()# dequeue and# ...do something with dataifSHT_HITS_THE_FAN:sc.stop()# instant off switch# end of this loop, as well as the streaming process from above# If neededq.clear()sc.clear()
示例3(OAuth和Twitter示例消防水龙带):
注意:此示例需要requests_oauthlib
importosimportjsonfromrequests_oauthlibimportOAuth1fromtidehunterimportHunterurl='https://stream.twitter.com/1.1/statuses/sample.json'auth=OAuth1(os.environ['TWITTER_CONSUMER_KEY'],os.environ['TWITTER_CONSUMER_SECRET'],os.environ['TWITTER_TOKEN_KEY'],os.environ['TWITTER_TOKEN_SECRET'])h=Hunter(url=url,q=q,auth=auth)r=h.tide_on(5)# let's just get 5 for nowprint(r.status_code)print('')whileh.q.qsize():print(json.loads(h.q.get()))print('')
您可以在this requests doc上找到其他身份验证。 简而言之,您只需传递所需的auth参数 到Hunter,就像使用requests所做的那样。
测试(单元测试)
$ pip install -r requirements.txt
$ pip install -r test_requirements.txt
$ nosetests --with-coverage --cover-package=tidehunter
许可证
麻省理工学院的执照。看到全部 LICENSE。
变更日志
1.0.1(2015-04-17)
- 中断更改:tidehunter.SimpleStateCounter更新为 反映`techies<;https://github.com/woozyking/techies>;`.2.0 对StateCounter的更改。
1.0.0(2014-01-22)
- 已将Queue,StateCounter的代码基移动到 techies。是推荐的 将techies与tidehunter一起使用,但并不总是这样 必需,因此不依赖于tidehunter
- 添加了tidehunter.SimpleStateCounter以便在没有其他 提供状态计数器。这是一个纯粹的进程内实现 因此其他进程无法访问
- 现在可以执行from tidehunter import Hunter,而不是 from tidehunter.stream import Hunter
- 将PycURL替换为
requests。一些
优点:
- 直接python 2/3支持
- 更清洁的实施
- 进一步委托various authentications support 到requests本身
0.1.9(2013-12-24)
- PyCurl和Redispython库已升级到最新版本 版本。
- Queue现在与几乎python队列兼容(在投诉中 自由时尚),除了Queue.full总是 返回False;Queue.task_done和Queue.join不执行任何操作。
- 新的:现在Queue和StateCounter都有一个clear方法 它对所述密钥执行redisDEL命令并 基于每个类的initialize方法重新初始化。
0.1.8(2013-10-02)
- 添加了别名方法put_nowait()和get_nowait()以及其他 占位符以映射python内置队列接口。
- 添加了rstgenshell脚本,用于标记到restructuredtext。到 使用,在根文件夹中运行$ source rstgen。
- 单元测试和演示中涉及的凭据现在正在使用环境 变量。
0.1.7(2013-07-22)
- 对readme.rst的大量更新
- 修复了长描述的pypi呈现。
0.1.5(2013-07-22)
- 新的:Hunter.tide_on()现在接受可选的限制参数 动态限制调整。调整不是永久的,意思是 如果你想重复使用猎人目标,旧限制(或 默认无)有效。
- 修正了亨特的一个潜在问题 限制。
- 添加了temp basic auth测试用例(没有流,需要找到更好的 来源)。
0.1.3(2013-07-13)
- 现在使用伟大的httpbin.org(Kenneth Reitz)进行单元测试。
- auth(oauth或basic)不再是必需的,只要目标 流服务器支持无需验证的访问。
0.1.2(2013-07-12)
- 包括要在pypi上显示的更改(changelog)。
- 使用WITH语句打开setup.py的文件。
- 添加了第一个 demo。
0.1.1(2013-07-12)
- 清理setup.py以确保安装/更新需求。
0.1.0(2013-07-12)
- 初始版本