Skip to content
This repository was archived by the owner on Oct 1, 2021. It is now read-only.

Commit 033c5ea

Browse files
DMT07fscherf
authored andcommitted
client: add ssl support; add documentation
Signed-off-by: Males Tomlinson <TomlinDM@eskom.co.za> Edited-by: Florian Scherf <f.scherf@pengutronix.de> Updated commit message
1 parent cb98377 commit 033c5ea

File tree

2 files changed

+66
-4
lines changed

2 files changed

+66
-4
lines changed

README.rst

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -260,6 +260,65 @@ For details see the corresponding Django documentation.
260260
)
261261
262262
263+
Using SSL Connections
264+
~~~~~~~~~~~~~~~~~~~~~
265+
If you need to setup a secure RPC server (use own certification files for example) you can create a ssl.SSLContext instance and pass it into the aiohttp web application.
266+
267+
The following code implements a simple secure RPC server that serves the method ``ping`` on ``localhost:8080``
268+
269+
.. code-block:: python
270+
271+
from aiohttp.web import Application, run_app
272+
from aiohttp_json_rpc import JsonRpc
273+
import asyncio
274+
import ssl
275+
276+
277+
async def ping(request):
278+
return 'pong'
279+
280+
281+
if __name__ == '__main__':
282+
loop = asyncio.get_event_loop()
283+
284+
rpc = JsonRpc()
285+
rpc.add_methods(
286+
('', ping),
287+
)
288+
289+
app = Application(loop=loop)
290+
app.router.add_route('*', '/', rpc.handle_request)
291+
292+
ssl_context = ssl.create_default_context(ssl.Purpose.CLIENT_AUTH)
293+
ssl_context.load_cert_chain('path/to/server.crt', 'path/to/server.key')
294+
295+
run_app(app, host='0.0.0.0', port=8080, ssl_context=ssl_context)
296+
297+
The following code implements a secure RPC client using the ``JsonRpcClient`` Python client.
298+
299+
.. code-block:: python
300+
301+
from aiohttp_json_rpc import JsonRpcClient
302+
import ssl
303+
304+
async def ping_json_rpc():
305+
"""Connect to wss://localhost:8080/, call ping() and disconnect."""
306+
rpc_client = JsonRpcClient()
307+
ssl_context = ssl.create_default_context(ssl.Purpose.CLIENT_AUTH)
308+
ssl_context.load_cert_chain('path/to/server.crt','path/to/server.key')
309+
try:
310+
await rpc_client.connect('localhost', 8080, ssl=ssl_context)
311+
call_result = await rpc_client.call('ping')
312+
print(call_result) # prints 'pong' (if that's return val of ping)
313+
finally:
314+
await rpc_client.disconnect()
315+
316+
317+
asyncio.get_event_loop().run_until_complete(ping_json_rpc())
318+
319+
See `aiohttp documentation <https://docs.aiohttp.org/en/stable/client_advanced.html#ssl-control-for-tcp-sockets>`_ for more details on SSL control for TCP sockets.
320+
321+
263322
Class References
264323
----------------
265324

aiohttp_json_rpc/client.py

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -115,14 +115,14 @@ async def _handle_msgs(self):
115115

116116
self._logger.debug('#%s: worker stopped', self._id)
117117

118-
async def connect_url(self, url, cookies=None):
118+
async def connect_url(self, url, cookies=None, ssl=None):
119119
url = URL(url)
120120
self._session = aiohttp.ClientSession(cookies=cookies, loop=self._loop)
121121

122122
self._logger.debug('#%s: ws connect...', self._id)
123123
self._ws = None
124124
try:
125-
self._ws = await self._session.ws_connect(url)
125+
self._ws = await self._session.ws_connect(url, ssl=ssl)
126126
finally:
127127
if self._ws is None:
128128
# Ensure session is closed when connection failed
@@ -131,9 +131,12 @@ async def connect_url(self, url, cookies=None):
131131

132132
self._message_worker = asyncio.ensure_future(self._handle_msgs())
133133

134-
async def connect(self, host, port, url='/', protocol='ws', cookies=None):
134+
async def connect(self, host, port, url='/', protocol='ws', cookies=None,
135+
ssl=None):
136+
if ssl is not None and protocol == 'ws':
137+
protocol = 'wss'
135138
url = URL.build(scheme=protocol, host=host, port=port, path=url)
136-
await self.connect_url(url, cookies=cookies)
139+
await self.connect_url(url, cookies=cookies, ssl=ssl)
137140

138141
async def auto_connect(self):
139142
if self._autoconnect_url is None:

0 commit comments

Comments
 (0)