Code Monkey home page Code Monkey logo

aiohttp's Introduction

http client/server for asyncio

aiohttp logo

https://secure.travis-ci.org/KeepSafe/aiohttp.png

Features

  • Supports both client and server side of HTTP protocol.
  • Supports Web-Sockets out-of-the-box.
  • Web-server has middlewares and pluggable routing.

Requirements

License

aiohttp is offered under the Apache 2 license.

Documentation

http://aiohttp.readthedocs.org/

Source code

The latest developer version is available in a github repository: https://github.com/KeepSafe/aiohttp

Getting started

Client

To retrieve something from the web:

import aiohttp

def get_body(url):
    response = yield from aiohttp.request('GET', url)
    return (yield from response.read())

You can use the get command like this anywhere in your asyncio powered program:

response = yield from aiohttp.request('GET', 'http://python.org')
body = yield from response.read()
print(body)

If you want to use timeouts for aiohttp client side please use standard asyncio approach:

yield from asyncio.wait_for(request('GET', url), 10)

Server

In aiohttp 0.12 we've added highlevel API for web HTTP server.

There is simple usage example:

import asyncio
from aiohttp import web


@asyncio.coroutine
def handle(request):
    name = request.match_info.get('name', "Anonymous")
    text = "Hello, " + name
    return web.Response(body=text.encode('utf-8'))


@asyncio.coroutine
def init(loop):
    app = web.Application(loop=loop)
    app.router.add_route('GET', '/{name}', handle)

    srv = yield from loop.create_server(app.make_handler(),
                                        '127.0.0.1', 8080)
    print("Server started at http://127.0.0.1:8080")
    return srv

loop = asyncio.get_event_loop()
loop.run_until_complete(init(loop))
loop.run_forever()

aiohttp's People

Contributors

asvetlov avatar fafhrd91 avatar kxepal avatar mind1m avatar popravich avatar flying-sheep avatar vharitonsky avatar ludovic-gasc avatar nbraem avatar saghul avatar tailhook avatar oohlaf avatar madjar avatar bayandin avatar ajdavis avatar zed avatar the-dud avatar spumer avatar landersson avatar wking avatar vaibhavsagar avatar sgranade avatar sffjunkie avatar robberphex avatar mikluko avatar martiusweb avatar lukmdo avatar l04m33 avatar imbolc avatar yhlam avatar

Watchers

James Cloos avatar DrLinux avatar

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.