File: client_auth.py

package info (click to toggle)
python-aiohttp 0.17.2-1~bpo8%2B1
  • links: PTS, VCS
  • area: main
  • in suites: jessie-backports
  • size: 2,368 kB
  • sloc: python: 19,899; makefile: 205
file content (28 lines) | stat: -rwxr-xr-x 718 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
import aiohttp
import asyncio


@asyncio.coroutine
def go(session):
    print('Query http://httpbin.org/basic-auth/andrew/password')
    resp = yield from session.get(
        'http://httpbin.org/basic-auth/andrew/password')
    print(resp.status)
    try:
        body = yield from resp.text()
        print(body)
    finally:
        yield from resp.release()


loop = asyncio.get_event_loop()
session = aiohttp.ClientSession(auth=aiohttp.BasicAuth('andrew',
                                                       'password'),
                                loop=loop)
loop.run_until_complete(go(session))
session.close()

# run loop iteration for actual session closing
loop.stop()
loop.run_forever()
loop.close()