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

added basic server docs and example #134

Merged
merged 2 commits into from
Aug 7, 2014
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Next Next commit
added basic server docs
  • Loading branch information
vharitonsky committed Aug 7, 2014
commit f8461d07b7ba442fcf6177f4b3668e76aeda2c85
1 change: 1 addition & 0 deletions docs/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ Contents:
:maxdepth: 2

client
server
api

Indices and tables
Expand Down
61 changes: 61 additions & 0 deletions docs/server.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
.. _server:

HTTP Server
===========

.. module:: aiohttp.client

Run a basic server
-------

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

.. code-block:: python

import aiohttp
import aiohttp.server

import asyncio

class HttpRequestHandler(aiohttp.server.ServerHttpProtocol):

@asyncio.coroutine
def handle_request(self, message, payload):
response = aiohttp.Response(
self.writer, 200, http_version=message.version
)
response.add_header('Content-type', 'text/plain')
response.send_headers()
response.write(b'Hello world!')
yield from response.write_eof()

All necessary data is passed to handle request in message and payload params.
Message contains HTTP request headers 'as is', payload is the body of the requests
wrapped in FlowControlStreamReader. To read the body of the request, you should
yield from payload's read method like so:

.. code-block:: python

msg = yield from payload.read()


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'
)
srv = loop.run_until_complete(f)
print('serving on', srv.sockets[0].getsockname())
try:
loop.run_forever()
except KeyboardInterrupt:
pass
28 changes: 28 additions & 0 deletions examples/basic_srv.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import aiohttp
import aiohttp.server

import asyncio

class HttpRequestHandler(aiohttp.server.ServerHttpProtocol):

@asyncio.coroutine
def handle_request(self, message, payload):
response = aiohttp.Response(
self.writer, 200, http_version=message.version
)
response.add_header('Content-type', 'text/plain')
response.send_headers()
response.write(b'Hello world!')
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