Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add more slides for AFPyro-BE training #1

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
.idea
pyvenv
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,10 @@ jump to a specific page.

Aaron Maxwell
amax at redsymbol dot net

## Contributors

Ludovic Gasc (GMLudo)

* https://twitter.com/GMLudo
* https://github.com/GMLudo
35 changes: 35 additions & 0 deletions examples/aiohttp_basic_server.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
"""Basic http server with minimal setup"""
import aiohttp
import aiohttp.server

import asyncio
from urllib.parse import urlparse, parse_qsl
from aiohttp.multidict import MultiDict


class HttpRequestHandler(aiohttp.server.ServerHttpProtocol):

@asyncio.coroutine
def handle_request(self, message, payload):
response = aiohttp.Response(
self.writer, 200, http_version=message.version)
content = "<h1>It Works!</h1>"
bcontent = content.encode('utf-8')
response.add_header('Content-Type', 'text/html; charset=UTF-8')
response.add_header('Content-Length', str(len(bcontent)))
response.send_headers()
response.write(bcontent)
yield from response.write_eof()


if __name__ == '__main__':
loop = asyncio.get_event_loop()
f = loop.create_server(
lambda: HttpRequestHandler(debug=True, keep_alive=75),
'0.0.0.0', '8080')
srv = loop.run_until_complete(f)
print('serving on', srv.sockets[0].getsockname())
try:
loop.run_forever()
except KeyboardInterrupt:
pass
20 changes: 20 additions & 0 deletions examples/aiohttp_client.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
#!/usr/bin/env python3

import aiohttp
import sys
import asyncio

@asyncio.coroutine
def curl(url):
response = yield from aiohttp.request('GET', url)
print(repr(response))

chunk = yield from response.content.read()
print('Downloaded: %s' % len(chunk))

response.close()


if __name__ == '__main__':
loop = asyncio.get_event_loop()
loop.run_until_complete(curl(sys.argv[1]))
44 changes: 44 additions & 0 deletions examples/aiohttp_complete_server.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
"""Basic http server with minimal setup"""
import aiohttp
import aiohttp.server

import asyncio
from urllib.parse import urlparse, parse_qsl
from aiohttp.multidict import MultiDict


class HttpRequestHandler(aiohttp.server.ServerHttpProtocol):

@asyncio.coroutine
def handle_request(self, message, payload):
response = aiohttp.Response(
self.writer, 200, http_version=message.version)
get_params = MultiDict(parse_qsl(urlparse(message.path).query))
if message.method == 'POST':
post_params = yield from payload.read()
else:
post_params = None
content = "<h1>It Works!</h1>"
if get_params:
content += "<h2>Get params</h2><p>" + str(get_params) + "</p>"
if post_params:
content += "<h2>Post params</h2><p>" + str(post_params) + "</p>"
bcontent = content.encode('utf-8')
response.add_header('Content-Type', 'text/html; charset=UTF-8')
response.add_header('Content-Length', str(len(bcontent)))
response.send_headers()
response.write(bcontent)
yield from response.write_eof()


if __name__ == '__main__':
loop = asyncio.get_event_loop()
f = loop.create_server(
lambda: HttpRequestHandler(debug=True, keep_alive=75),
'0.0.0.0', '8080')
srv = loop.run_until_complete(f)
print('serving on', srv.sockets[0].getsockname())
try:
loop.run_forever()
except KeyboardInterrupt:
pass
Loading