mosquito / aiohttp-xmlrpc

XMLRPC for aiohttp
MIT License
34 stars 19 forks source link

Pass SSL through parameter to aiohttp client #18

Closed Jared-Otterstatter closed 6 years ago

Jared-Otterstatter commented 6 years ago

I needed to pass custom ssl certs to xml rpc servers, and thought it could be of useful to others

mosquito commented 6 years ago

It's wrong way. You should pass kwargs for client which will be passed to aiohttp.ClientSession or pass configured client as an argument.

I have to revert this changes.

Jared-Otterstatter commented 6 years ago

Here when you call the ClientSession constructor there doesn't seem to be a keyword arg for ssl. However in the request method, there is a ssl keyword arg, similar to the headers arg which you already are using. I thought I would imitate the way you handled headers.

acspike commented 6 years ago

@mosquito, I'd like to use aiohttp_xmlrpc, but I need something like the change proposed in this pull request to enable ssl in the client. Could you take another look at this?

mosquito commented 6 years ago

@Jared-Otterstatter you can use already initialized client session:


import asyncio
from aiohttp_xmlrpc.client import ServerProxy

loop = asyncio.get_event_loop()

async def main():
    async with aiohttp.ClientSession(ssl=...) as session:
        client = ServerProxy("http://127.0.0.1:8080/", client=session)
        print(await client.test())

        # Or via __getitem__
        method = client['args']
        print(await method(1, 2, 3))
  # no need to close client here

if __name__ == "__main__":
    loop.run_until_complete(main())
Jared-Otterstatter commented 6 years ago

This is the error I got from running your example:

Traceback (most recent call last):
  File "otherTestSSLXML_client.py", line 28, in <module>
    loop.run_until_complete(main())
  File "/usr/lib/python3.5/asyncio/base_events.py", line 466, in run_until_complete
    return future.result()
  File "/usr/lib/python3.5/asyncio/futures.py", line 293, in result
    raise self._exception
  File "/usr/lib/python3.5/asyncio/tasks.py", line 239, in _step
    result = coro.send(None)
  File "otherTestSSLXML_client.py", line 17, in main
    async with aiohttp.ClientSession(ssl=sslContext) as session:
TypeError: __init__() got an unexpected keyword argument 'ssl'
mosquito commented 6 years ago

@Jared-Otterstatter previous example and your changes the same. That's because you should pass ssl context to the TCPConnector.

On my opinion this is no RPC client responsibility. You should pass preconfigured connector to the ClientSession.


import asyncio
import aiohttp
from aiohttp_xmlrpc.client import ServerProxy

loop = asyncio.get_event_loop()

async def main():
    async with aiohttp.ClientSession(connector=aiohttp.TCPConnector(ssl=...)) as session:
        client = ServerProxy("http://127.0.0.1:8080/", client=session)
        print(await client.test())

        # Or via __getitem__
        method = client['args']
        print(await method(1, 2, 3))
  # no need to close client here

if __name__ == "__main__":
    loop.run_until_complete(main())
Jared-Otterstatter commented 6 years ago

I see, that works. Thank you for your help and explanations.