具有精确流控制的http流

tidehunter的Python项目详细描述


潮架

具有精确流控制的http流

主分支:Build Status

注意:自1.x起不向后兼容0.x。

亮点

  • 消费限制,完全控制你的流量配额 客户端。
  • 即时开关和精确的消耗计数器。最佳使用 用techies
  • 可伸缩流数据消费的队列接口。最佳搭配 techies
  • 基于实体的核心机制 requests图书馆, 继承它所有的优点。

安装

$ pip install tidehunter
$ pip install tidehunter --upgrade

使用量

示例1(带限制):

注意:当没有提供外部QueueStateCounter时, 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)

1.0.0(2014-01-22)

  • 已将QueueStateCounter的代码基移动到 techies。是推荐的 将techiestidehunter一起使用,但并不总是这样 必需,因此不依赖于tidehunter
  • 添加了tidehunter.SimpleStateCounter以便在没有其他 提供状态计数器。这是一个纯粹的进程内实现 因此其他进程无法访问
  • 现在可以执行from tidehunter import Hunter,而不是 from tidehunter.stream import Hunter
  • PycURL替换为 requests。一些 优点:

0.1.9(2013-12-24)

  • PyCurlRedispython库已升级到最新版本 版本。
  • Queue现在与几乎python队列兼容(在投诉中 自由时尚),除了Queue.full总是 返回FalseQueue.task_doneQueue.join不执行任何操作。
  • 新的:现在QueueStateCounter都有一个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)

  • 初始版本

欢迎加入QQ群-->: 979659372 Python中文网_新手群

推荐PyPI第三方库


热门话题
java在ArrayList中比较数字   java在Kotlin中使异步调用同步   让“Scala编程”junit示例在IntelliJ中工作的java问题   java Servlet侦听器未在ContextListener中设置属性   将Microsoft SQL Server数据库连接到我的Java项目   加载资源时出现java“需要注册工厂”异常   java如何使用POI检查excel中的重复记录?   java如何更改机器生成的代码   java如何确保重写的方法是同步的   用Spring编写Hibernate时的java XML奥秘   java管理mysql数据库中存储的用户权限   java如何运行。来自Javascript的jar方法   java我想在Web应用程序中进行身份验证&对桌面应用程序使用相同的凭据。我该怎么做?