diff --git a/1.py b/1.py new file mode 100644 index 00000000000..054621fbcdd --- /dev/null +++ b/1.py @@ -0,0 +1,8 @@ +import asyncio +import aiohttp + +async def fetch(): + async with aiohttp.ClientSession() as session: + await session.get('https://雜草工作室.香港') + +asyncio.get_event_loop().run_until_complete(fetch()) diff --git a/2.py b/2.py new file mode 100644 index 00000000000..d17de5bab19 --- /dev/null +++ b/2.py @@ -0,0 +1,11 @@ +from aiohttp import web + +async def handle(request): + return web.Response(text='

testing

', content_type='text/html') + +app = web.Application() +app.router.add_get('/', handle) + + +if __name__ == '__main__': + web.run_app(app, port=8001) diff --git a/3.py b/3.py new file mode 100644 index 00000000000..f5a81efb765 --- /dev/null +++ b/3.py @@ -0,0 +1,46 @@ +import asyncio +import aiohttp +import uvloop +import time +import logging + +from aiohttp import ClientSession, TCPConnector + +logging.basicConfig(level=logging.DEBUG) +logger = logging.getLogger() + +urls = ["http://www.yahoo.com","http://www.bbcnews.com","http://www.cnn.com","http://www.buzzfeed.com","http://www.walmart.com","http://www.emirates.com","http://www.kayak.com","http://www.expedia.com","http://www.apple.com","http://www.youtube.com"] +bigurls = 10 * urls + +def run(enable_uvloop): + try: + if enable_uvloop: + loop = uvloop.new_event_loop() + else: + loop = asyncio.new_event_loop() + asyncio.set_event_loop(loop) + start = time.time() + conn = TCPConnector(limit=5000, use_dns_cache=True, loop=loop, verify_ssl=False) + with ClientSession(connector=conn) as session: + tasks = asyncio.gather(*[asyncio.ensure_future(do_request(url, session)) for url in bigurls]) # tasks to do + results = loop.run_until_complete(tasks) # loop until done + end = time.time() + logger.debug('total time:') + logger.debug(end - start) + return results + loop.close() + except Exception as e: + logger.error(e, exc_info=True) + +async def do_request(url, session): + """ + """ + try: + async with session.get(url) as response: + resp = await response.text() + return resp + except Exception as e: + logger.error(e, exc_info=True) + +#run(True) +run(False) diff --git a/4.py b/4.py new file mode 100644 index 00000000000..f15c5f803b9 --- /dev/null +++ b/4.py @@ -0,0 +1,34 @@ +#!/usr/bin/env python +# run it as: +# python -m unittest test_example.TestClientCase + +from aiohttp import web +from aiohttp.test_utils import AioHTTPTestCase, unittest_run_loop +from aiohttp.test_utils import TestClient, TestServer + + +class TestClientCase(AioHTTPTestCase): + def _app_index(self, request): + return web.Response(body="Here we are` via *middlewares*. -*Middlewares* are setup by providing a sequence of *middleware factories* to -the keyword-only ``middlewares`` parameter when creating an -:class:`Application`:: - - app = web.Application(middlewares=[middleware_factory_1, - middleware_factory_2]) - -A *middleware* is just a coroutine that can modify either the request or +A *middleware* is a coroutine that can modify either the request or response. For example, here's a simple *middleware* which appends ``' wink'`` to the response:: @@ -325,6 +317,12 @@ response. For example, here's a simple *middleware* which appends Every *middleware* should accept two parameters, a :class:`request ` instance and a *handler*, and return the response. +When creating an :class:`Application`, these *middlewares* are passed to +the keyword-only ``middlewares`` parameter:: + + app = web.Application(middlewares=[middleware_1, + middleware_2]) + Internally, a single :ref:`request handler ` is constructed by applying the middleware chain to the original handler in reverse order, and is called by the :class:`RequestHandler` as a regular *handler*. diff --git a/notes.txt b/notes.txt new file mode 100644 index 00000000000..9cee8e1ab82 --- /dev/null +++ b/notes.txt @@ -0,0 +1,8 @@ +Google analytics +Add Python to main page +v:stable doesn't work +Mention "Latest stable" in Library Installation, version number and release date + +Add asyncio to Title +Add PyPI badge +hotjar diff --git a/run_aiohttp.py b/run_aiohttp.py new file mode 100644 index 00000000000..b80b5b94be9 --- /dev/null +++ b/run_aiohttp.py @@ -0,0 +1,26 @@ +import asyncio + +import ujson +from aiohttp import web + + +async def index(request): + data = {'message': 'hello world'} + body = ujson.dumps(data) + return web.Response(body=body.encode(), content_type='application/json') + + +# import tokio +# asyncio.set_event_loop_policy(tokio.TokioLoopPolicy()) + +import uvloop +asyncio.set_event_loop_policy(uvloop.EventLoopPolicy()) + +app = web.Application() +# app.on_startup.append(startup) +# app.on_cleanup.append(cleanup) +app.router.add_get('/', index) +# app.router.add_get('/db', db) + +# web.run_app(app, port=8000) +web.run_app(app, port=8000, access_log=None) diff --git a/run_aiohttp2.py b/run_aiohttp2.py new file mode 100644 index 00000000000..5c34f24345e --- /dev/null +++ b/run_aiohttp2.py @@ -0,0 +1,11 @@ +import asyncio +from aiohttp import web + +async def hello(request): + return web.Response(body=b"Hello, world") + +app = web.Application() +app.router.add_route('GET', '/', hello) + +if __name__ == '__main__': + web.run_app(app) diff --git a/run_aiohttp3.py b/run_aiohttp3.py new file mode 100644 index 00000000000..45ebdb85a6f --- /dev/null +++ b/run_aiohttp3.py @@ -0,0 +1,15 @@ +import asyncio +from aiohttp import web + +import uvloop +asyncio.set_event_loop_policy(uvloop.EventLoopPolicy()) + + +async def hello(request): + return web.Response(body=b"Hello, world") + +app = web.Application() +app.router.add_route('GET', '/', hello) + +if __name__ == '__main__': + web.run_app(app) diff --git a/run_aiohttp4.py b/run_aiohttp4.py new file mode 100644 index 00000000000..6906bef3ad6 --- /dev/null +++ b/run_aiohttp4.py @@ -0,0 +1,15 @@ +import asyncio +from aiohttp import web + +import uvloop +asyncio.set_event_loop_policy(uvloop.EventLoopPolicy()) + + +async def hello(request): + return web.Response(body=b"Hello, world") + +app = web.Application() +app.router.add_route('GET', '/', hello) + +if __name__ == '__main__': + web.run_app(app, access_log=None)