Skip to content

Instantly share code, notes, and snippets.

@asvetlov
Last active August 29, 2015 14:26
Show Gist options
  • Save asvetlov/218a1d7d05a540d70a21 to your computer and use it in GitHub Desktop.
Save asvetlov/218a1d7d05a540d70a21 to your computer and use it in GitHub Desktop.
aiohttp client with cookies
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