Last active
December 30, 2017 16:42
-
-
Save tomjakubowski/5674ed1948d3cbf338acf92fcf178177 to your computer and use it in GitHub Desktop.
aiohttp client example for python 3.5
This file contains hidden or 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
# updated from http://stackabuse.com/python-async-await-tutorial/ | |
# still doesn't properly deal with KeyboardInterrupt OMM :( | |
import signal | |
import sys | |
import asyncio | |
import aiohttp | |
import json | |
async def get_json(client, url): | |
async with client.get(url) as response: | |
assert response.status == 200 | |
return await response.read() | |
async def get_reddit_top(subreddit, client): | |
data1 = await get_json(client, 'https://www.reddit.com/r/' + subreddit + | |
'/top.json?sort=top&t=day&limit=5') | |
j = json.loads(data1.decode('utf-8')) | |
for i in j['data']['children']: | |
score = i['data']['score'] | |
title = i['data']['title'] | |
link = i['data']['url'] | |
print(str(score) + ': ' + title + ' (' + link + ')') | |
print('DONE:', subreddit + '\n') | |
# def signal_handler(signal, frame): | |
# print('we get sig') | |
# loop.stop() | |
# client.close() | |
# sys.exit(0) | |
async def main(): | |
async with aiohttp.ClientSession(loop=loop) as client: | |
await asyncio.wait([ | |
get_reddit_top('python', client), | |
get_reddit_top('programming', client), | |
get_reddit_top('compsci', client) | |
]) | |
loop = asyncio.get_event_loop() | |
# signal.signal(signal.SIGINT, signal_handler) | |
loop.run_until_complete(main()) |
This file contains hidden or 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
#!/usr/bin/env python3 | |
# -*- coding: utf-8; flycheck-python-pycompile-executable: "python3"; python-shell-interpreter: "python3"; -*- | |
# stolen from the docs -- this example *does* handle KeyboardInterrupt properly OMM. | |
# What's different? | |
import asyncio | |
class announce(): | |
""" Return message uppercase """ | |
def __init__(self): | |
""" Start the tcp server """ | |
host = "localhost" | |
port = 2000 | |
loop = asyncio.get_event_loop() | |
coro = asyncio.start_server(self.TCPRequestHandler, host, port, loop=loop) | |
self.msg("A") | |
server = loop.run_until_complete(coro) | |
self.msg("B") | |
try: | |
loop.run_forever() | |
except: | |
server.close() | |
self.msg("C") | |
loop.run_until_complete(server.wait_closed()) | |
loop.close() | |
async def TCPRequestHandler(self, reader, writer): | |
self.msg("Entered request, awaiting") | |
data = await reader.read(100) | |
message = data.decode("utf-8") | |
addr = writer.get_extra_info('peername') | |
self.msg("Received {0} from {1}".format(message,addr)) | |
writer.write(data.upper()) | |
await writer.drain() | |
writer.close() | |
def msg(self, message): | |
print (message) | |
def main(): | |
announce() | |
if __name__ == "__main__": | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment