Skip to content

Commit

Permalink
Added some clarity to the http client example, and fixed the one with…
Browse files Browse the repository at this point in the history
… the web sockets in the readme file.
  • Loading branch information
dkuznetsov committed Apr 12, 2015
1 parent 005b873 commit 63052bb
Showing 1 changed file with 15 additions and 5 deletions.
20 changes: 15 additions & 5 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -54,11 +54,18 @@ To retrieve something from the web:
.. code-block:: python
import aiohttp
import asyncio
def get_body(url):
response = yield from aiohttp.request('GET', url)
return (yield from response.read())
if __name__ == '__main__':
loop = asyncio.get_event_loop()
raw_html = loop.run_until_complete(get_body('http://python.org'))
print(raw_html)
You can use the get command like this anywhere in your ``asyncio``
powered program:

Expand Down Expand Up @@ -88,33 +95,36 @@ This 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 wshandler(request):
ws = WebSocketResponse()
ws = web.WebSocketResponse()
ws.start(request)
while True:
msg = yield from ws.receive()
if msg.tp == web.MsgType.text:
ws.send_str(msg.data)
ws.send_str("Hello, {}".format(msg.data))
elif msg.tp == web.MsgType.binary:
ws.send_bytes(msg.data)
ws.send_bytes(msg.data)
elif msg.tp == web.MsgType.close:
break
break
return ws
@asyncio.coroutine
def init(loop):
app = web.Application(loop=loop)
app.router.add_route('GET', '/echo', wshandle)
app.router.add_route('GET', '/echo', wshandler)
app.router.add_route('GET', '/{name}', handle)
srv = yield from loop.create_server(app.make_handler(), '127.0.0.1', 8080)
Expand Down

0 comments on commit 63052bb

Please sign in to comment.