Debian Codegraph @

examples/example_ssl.py

raw
1 import asyncio
2 import ssl
3 import aiomysql
4
5 ctx = ssl.SSLContext(ssl.PROTOCOL_TLSv1)
6 ctx.check_hostname = False
7 ctx.load_verify_locations(cafile='../tests/ssl_resources/ssl/ca.pem')
8
9
10 async def main():
11 async with aiomysql.create_pool(
12 host='localhost', port=3306, user='root',
13 password='rootpw', ssl=ctx,
14 auth_plugin='mysql_clear_password') as pool:
15
16 async with pool.acquire() as conn:
17 async with conn.cursor() as cur:
18 # Run simple command
19 await cur.execute("SHOW DATABASES;")
20 value = await cur.fetchall()
21
22 values = [item[0] for item in value]
23 # Spot check the answers, we should at least have mysql
24 # and information_schema
25 assert 'mysql' in values, \
26 'Could not find the "mysql" table'
27 assert 'information_schema' in values, \
28 'Could not find the "mysql" table'
29
30 # Check TLS variables
31 await cur.execute("SHOW STATUS LIKE 'Ssl_version%';")
32 value = await cur.fetchone()
33
34 # The context has TLS
35 assert value[1].startswith('TLS'), \
36 'Not connected to the database with TLS'
37
38 asyncio.get_event_loop().run_until_complete(main())