Skip to content

Commit

Permalink
Reflow docs, improve basic http server example.
Browse files Browse the repository at this point in the history
  • Loading branch information
asvetlov committed Aug 7, 2014
1 parent 34a4395 commit c5f350e
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 36 deletions.
37 changes: 18 additions & 19 deletions docs/server.rst
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ HTTP Server
.. module:: aiohttp.client

Run a basic server
-------
------------------

Start implementing the basic server by inheriting
Start implementing the basic server by inheriting
aiohttp.server.ServerHttpProtocol object. Your class
should implement the only method handle_request which must
be a coroutine to handle requests asynchronously
Expand Down Expand Up @@ -37,36 +37,37 @@ be a coroutine to handle requests asynchronously
response.write(b'<h1>It Works!</h1>')
yield from response.write_eof()
Next step is creating a loop and registering your handler within a server.
KeyboardInterrupt exception handling is necessary so you can stop
Next step is creating a loop and registering your handler within a server.
KeyboardInterrupt exception handling is necessary so you can stop
your server with Ctrl+C at any time.

.. code-block:: python
if __name__ == '__main__':
loop = asyncio.get_event_loop()
f = loop.create_server(
lambda: HttpRequestHandler(debug=True, keep_alive=75), '0.0.0.0', '8080'
)
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
Headers
-------
Request data is passed to handler in the ``message`` , while request body is passed in ``payload`` param.
HTTP headers are accessed through ``headers`` member of the message.
To check what current request method is, use ``method`` member of the ``message``. It should be one of
``GET``, ``POST``, ``PUT`` or ``DELETE`` strings.
Headers ------- Request data is passed to handler in the ``message`` ,
while request body is passed in ``payload`` param. HTTP headers are
accessed through ``headers`` member of the message. To check what
current request method is, use ``method`` member of the
``message``. It should be one of ``GET``, ``POST``, ``PUT`` or
``DELETE`` strings.

Handling GET params
-------
-------------------

Currently aiohttp does not provide automatical parsing of incoming GET params.
However aiohttp does provide a nice MulitiDict wrapper for already parsed params.
Currently aiohttp does not provide automatical parsing of incoming GET
params. However aiohttp does provide a nice MulitiDict wrapper for
already parsed params.


.. code-block:: python
Expand All @@ -87,9 +88,9 @@ However aiohttp does provide a nice MulitiDict wrapper for already parsed params
Handling POST data
-------
------------------

POST data is accessed through the ``payload.read()`` generator method.
POST data is accessed through the ``payload.read()`` generator method.
If you have form data in the request body, you can parse it the same way as
GET params.

Expand All @@ -109,5 +110,3 @@ GET params.
data = yield from payload.read()
post_params = MultiDict(parse_qsl(data))
print("Passed in POST", post_params)
42 changes: 25 additions & 17 deletions examples/basic_srv.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,28 +5,36 @@
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))
print("Passed in GET", get_params)
if message.method == 'POST':
data = yield from payload.read()
print("Passed in POST", MultiDict(parse_qsl(data)))
response.add_header('Content-Type', 'text/html')
response.add_header('Content-Length', '18')
response.send_headers()
response.write(b'<h1>It Works!</h1>')
yield from response.write_eof()

@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'
)
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:
Expand Down

0 comments on commit c5f350e

Please sign in to comment.