Last active
August 29, 2015 14:26
-
-
Save asvetlov/218a1d7d05a540d70a21 to your computer and use it in GitHub Desktop.
aiohttp client with cookies
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import aiohttp | |
import asyncio | |
class MyClass: | |
def __init__(self): | |
self.data = {'username': 'me', 'password': 'pw'} | |
self.login_url = 'http://example.com/login' | |
self.client = aiohttp.ClientSession() | |
@asyncio.coroutine | |
def call(self, url): | |
resp = yield from self.client.get(url) | |
try: | |
return (yield from resp.text()) | |
finally: | |
yield from resp.release() | |
@asyncio.coroutine | |
def login(self): | |
resp = yield from self.client.get(self.login_url, | |
data=aiohttp.FormData(self.data)) | |
yield from resp.release() | |
loop = asyncio.get_event_loop() | |
mc = MyClass() | |
loop.run_until_complete(mc.login()) | |
loop.run_until_complete(mc.call('http://example.com/other_url')) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment