Code Monkey home page Code Monkey logo

trip's Introduction

Trip: Async HTTP for Humans

pypi

TRIP, Tornado & Requests In Pair, an async HTTP library for Python.

Simple as Requests, Trip let you get rid of annoying network blocking.

Coroutine in python 2.7+ can be this simple:

import trip

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

trip.run(main)

With Trip, you may finish one hundred requests in one piece of time.

Trip gets its name from two powerful site packages and aims to combine them together. Trip refers to 'Tornado & Requests In Pair', TRIP. To put them together, I reused much of their codes about structure and dealing. Actually I only made little effort to make a mixture. Thanks to Tornado and Requests.

Through using Trip, you may take full advantage of Requests, including: Sessions with Cookie persistence, browser-style SSL verification, automatic content decoding, basic/digest authentication, elegant key/value Cookies. Meanwhile, your requests are coroutine like using AsyncHTTPClient of Tornado, network blocking will not be a problem.

Found difficult optimizing spiders' time consuming? Found tricky using asyncio http packages? Found heavy custimizing big spider framework? Try Trip, you will not regret!

Installation

Paste it into your console and enjoy:

python -m pip install trip

Documents

Documents are here: http://trip.readthedocs.io/zh/latest/

Advanced usage

Some of the advaced features are listed here:

Using async and await in python 3

import trip

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

trip.run(main)

Sessions with Cookie persistence

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)

Event hooks

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)

Timeouts

import trip

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

trip.run(main)

Proxy

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)

How to contribute

  1. You may open an issue to share your ideas with me.
  2. Or fork this project and do it your own on master branch.
  3. Please write demo codes of bugs or new features. You know, codes help.
  4. Finally if you finish your work and make a pull request, I will merge it in time after essential tests.

Similiar projects

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.