05bit / peewee-async

Asynchronous interface for peewee ORM powered by asyncio
http://peewee-async-lib.readthedocs.io
MIT License
727 stars 98 forks source link

Update docs for connect to database with ssl #171

Open JackTheMico opened 2 years ago

JackTheMico commented 2 years ago

Hi, I didn't find the related doc about connect with ssl in peewee-async and the method in peewee is not working. So I made some effort to figure it out. Could you please update it to the documents? It could be useful to people who's going to use database like PlanetScale.

You can see I created two testing scripts below, one is using dict for ssl, the other is using ssl.SSLContext.

ssl_connect

If I run test_ssl.py which uses dict for ssl, it will cause error:

python spiders/test_ssl.py
/home/dlwxxxdlw/codes/jackdeng/eastmoney/spiders/test_ssl.py:24: DeprecationWarning: There is no current event loop
  loop = asyncio.get_event_loop()
SSL handshake failed
protocol: <asyncio.sslproto.SSLProtocol object at 0x7f47f457ae60>
transport: <_SelectorSocketTransport fd=7 read=idle write=<idle, bufsize=0>>
Traceback (most recent call last):
  File "/usr/lib/python3.10/asyncio/sslproto.py", line 637, in _on_handshake_complete
    raise handshake_exc
  File "/usr/lib/python3.10/asyncio/sslproto.py", line 682, in _process_write_backlog
    ssldata = self._sslpipe.do_handshake(
  File "/usr/lib/python3.10/asyncio/sslproto.py", line 116, in do_handshake
    self._sslobj = self._context.wrap_bio(
AttributeError: 'dict' object has no attribute 'wrap_bio'
Future exception was never retrieved
future: <Future finished exception=OperationalError(2003, "Can't connect to MySQL server on ''")>
Traceback (most recent call last):
  File "/home/dlwxxxdlw/.cache/pypoetry/virtualenvs/eastmoney-UoyfY-mu-py3.10/lib/python3.10/site-packages/aiomysql/connection.py", line 540, in _connect
    await self._request_authentication()
  File "/home/dlwxxxdlw/.cache/pypoetry/virtualenvs/eastmoney-UoyfY-mu-py3.10/lib/python3.10/site-packages/aiomysql/connection.py", line 758, in _request_authentication
    self._reader, self._writer = await _open_connection(
  File "/home/dlwxxxdlw/.cache/pypoetry/virtualenvs/eastmoney-UoyfY-mu-py3.10/lib/python3.10/site-packages/aiomysql/connection.py", line 88, in _open_connection
    transport, _ = await loop.create_connection(
  File "/usr/lib/python3.10/asyncio/base_events.py", line 1089, in create_connection
    transport, protocol = await self._create_connection_transport(
  File "/usr/lib/python3.10/asyncio/base_events.py", line 1119, in _create_connection_transport
    await waiter
  File "/usr/lib/python3.10/asyncio/sslproto.py", line 637, in _on_handshake_complete
    raise handshake_exc
  File "/usr/lib/python3.10/asyncio/sslproto.py", line 682, in _process_write_backlog
    ssldata = self._sslpipe.do_handshake(
  File "/usr/lib/python3.10/asyncio/sslproto.py", line 116, in do_handshake
    self._sslobj = self._context.wrap_bio(
AttributeError: 'dict' object has no attribute 'wrap_bio'

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "/home/dlwxxxdlw/.cache/pypoetry/virtualenvs/eastmoney-UoyfY-mu-py3.10/lib/python3.10/site-packages/peewee_async.py", line 868, in connect_async
    await conn.connect()
  File "/home/dlwxxxdlw/.cache/pypoetry/virtualenvs/eastmoney-UoyfY-mu-py3.10/lib/python3.10/site-packages/peewee_async.py", line 1195, in connect
    self.pool = await aiomysql.create_pool(
  File "/home/dlwxxxdlw/.cache/pypoetry/virtualenvs/eastmoney-UoyfY-mu-py3.10/lib/python3.10/site-packages/aiomysql/pool.py", line 29, in _create_pool
    await pool._fill_free_pool(False)
  File "/home/dlwxxxdlw/.cache/pypoetry/virtualenvs/eastmoney-UoyfY-mu-py3.10/lib/python3.10/site-packages/aiomysql/pool.py", line 182, in _fill_free_pool
    conn = await connect(echo=self._echo, loop=self._loop,
  File "/home/dlwxxxdlw/.cache/pypoetry/virtualenvs/eastmoney-UoyfY-mu-py3.10/lib/python3.10/site-packages/aiomysql/connection.py", line 75, in _connect
    await conn._connect()
  File "/home/dlwxxxdlw/.cache/pypoetry/virtualenvs/eastmoney-UoyfY-mu-py3.10/lib/python3.10/site-packages/aiomysql/connection.py", line 558, in _connect
    raise OperationalError(2003,
pymysql.err.OperationalError: (2003, "Can't connect to MySQL server on ''")
Failed with dict value for ssl params.
(2003, "Can't connect to MySQL server on ''")

But if I run test_ssl_2.py which uses ssl.SSLContext and it will work:

python spiders/test_ssl_2.py
/home/dlwxxxdlw/codes/jackdeng/eastmoney/spiders/test_ssl_2.py:7: DeprecationWarning: ssl.PROTOCOL_TLS is deprecated
  ctx = ssl.SSLContext(ssl.PROTOCOL_SSLv23)
/home/dlwxxxdlw/codes/jackdeng/eastmoney/spiders/test_ssl_2.py:29: DeprecationWarning: There is no current event loop
  loop = asyncio.get_event_loop()
objects amount: 231
Success connect to planetscale with ssl.SSLContext
kalombos commented 5 months ago

You can do it yourself. I think the best place for it is here You just need to make a PR. Thanks for your contribution.