69

Trip:让协程与网络服务人类

 6 years ago
source link: https://github.com/littlecodersh/trip/blob/master/README_CN.md?amp%3Butm_medium=referral
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

Trip: 让协程与网络服务人类

Trip,Tornado & Requests In Pair,一个协程的网络库。

与 Requests 同样的操作,Trip让你摆脱网络延时阻塞程序。

Python的协程可以这么简单(兼容Python 2.7-3.7):

import trip

def main():
    r = yield trip.get('https://httpbin.org/get', auth=('user', 'pass'))
    print(r.content)

trip.run(main)

有了协程,同样的代码量,一百份请求一份时间

Trip的名字来源于其两个依赖包,也旨在将两个包的内容融合起来:'Tornado & Requests In Pair'。 在兼容中使用了大量上述两个包结构和处理的代码,我只是做了一些简单的整合工作,感谢 TornadoRequests让我能如此轻易的完成本项目的编写。

通过使用Trip,你可以充分使用Requests的各种特性,包括但不限于:带持久 Cookie 的会话、 浏览器式的 SSL 认证、自动内容解码、 基本/摘要式的身份认证、 优雅的 key/value Cookie。 同时你的请求又和使用Tornado的AsyncHTTPClient一般是协程的,网络延时不再会阻塞你的 程序,在程序正常运行的时候你可以同时等待多项任务的完成。

爬虫耗时太久优化困难吗?各种协程网络框架难以使用吗?大型爬虫框架臃肿无法灵活定制吗? 试试Trip,你不会后悔的!

无论你是使用的2.7,3.3,3.7,Trip都可以完美运行。

安装Trip非常简单,只需要在命令行中输入:

python -m pip install trip

你可以在这里找到本项目详细的文档。

如果在阅读文档过程当中遇到了问题, 也可以加入qq群与我们讨论:462703741。

这里展示部分的进阶应用:

使用async与await

import trip

async def main():
    r = await trip.get('https://httpbin.org/get', auth=('user', 'pass'))
    print(r.content)

trip.run(main)

Cookie的持久化

import trip

def main():
    s = trip.Session()
    r = yield s.get(
        'https://httpbin.org/cookies/set',
        params={'name': 'value'},
        allow_redirects=False)
    r = yield s.get('https://httpbin.org/cookies')
    print(r.content)

trip.run(main)

事件挂钩

import trip

def main():
    def print_url(r, *args, **kwargs):
        print(r.url)
    def record_hook(r, *args, **kwargs):
        r.hook_called = True
        return r
    url = 'http://httpbin.org/get'
    r = yield trip.get('http://httpbin.org', hooks={'response': [print_url, record_hook]})
    print(r.hook_called)

trip.run(main)

超时

import trip

def main():
    r = yield trip.get('http://github.com', timeout=0.001)
    print(r)

trip.run(main)

代理

import trip

proxies = {
    'http': '127.0.0.1:8080',
    'https': '127.0.0.1:8081',
}

def main():
    r = yield trip.get('https://httpbin.org/get', proxies=proxies)
    print(r.content)

trip.run(main)

如何贡献代码

  1. 你可以开启issue与我交流你的想法。
  2. 或者fork这个项目并在 master 分支上进行你的修改。
  3. 请务必带上出现问题或者新功能的相关代码,这会给我们的交流带来巨大的帮助。
  4. 最后如果你完成了修改可以通过pull request的方式提交,我会尽快完成测试并合并。

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK