Skip to content

Commit

Permalink
update file structure
Browse files Browse the repository at this point in the history
  • Loading branch information
Joxos committed Feb 16, 2024
1 parent 03889d3 commit 62b76f7
Show file tree
Hide file tree
Showing 16 changed files with 23 additions and 18 deletions.
File renamed without changes.
41 changes: 23 additions & 18 deletions pyasync_server/client/client.py → client/client.py
Original file line number Diff line number Diff line change
@@ -1,39 +1,43 @@
'''
"""
client.py: High-performance async client codes.
'''
"""
import asyncio
import ssl
import sys

sys.path.append('..')
sys.path.append("..")
from common.protocol import on_init, is_framed
from common.utils import show_status, compress, decompress, STATUS, handle_run_main, logger
from common.utils import (
show_status,
compress,
decompress,
STATUS,
handle_run_main,
logger,
)
from package import *
from config import *


class ClientProtocol(asyncio.Protocol):

def __init__(self, package_to_send, on_con_lost):
self.package_to_send = package_to_send
self.on_con_lost = on_con_lost
self.recieved_data = ''
self.recieved_data = ""
on_init(self)

def connection_made(self, transport):
self.transport = transport
self.address = transport.get_extra_info('peername')
self.address = transport.get_extra_info("peername")
show_status(STATUS.CONNECTED, self.address)
transport.write(
compress(bytes(self.package_to_send, encoding=default_coding)))
transport.write(compress(bytes(self.package_to_send, encoding=default_coding)))
show_status(STATUS.SEND, self.address, self.package_to_send)

def data_received(self, more_data):
try:
self.recieved_data += decompress(more_data).decode('utf-8')
self.recieved_data += decompress(more_data).decode("utf-8")
except:
show_status(STATUS.ERROR, self.address,
'Failed to decompress or decode.')
show_status(STATUS.ERROR, self.address, "Failed to decompress or decode.")
self.transport.close()
return
if is_framed(self):
Expand All @@ -56,28 +60,29 @@ async def main():
try:
context.load_verify_locations(crt_path)
except FileNotFoundError:
logger.error(f'File missing when using TLS.')
logger.error(f"File missing when using TLS.")
return
else:
logger.info('TLS enabled.')
logger.info("TLS enabled.")
else:
logger.warning('TLS not enabled.')
logger.warning("TLS not enabled.")

loop = asyncio.get_running_loop()
on_con_lost = loop.create_future()
mypackage = pack_request_mariadb_test('show databases')
mypackage = pack_request_mariadb_test("show databases")

transport, protocol = await loop.create_connection(
lambda: ClientProtocol(mypackage, on_con_lost),
server_address[0],
server_address[1],
ssl=context)
ssl=context,
)

try:
await on_con_lost
finally:
transport.close()


if __name__ == '__main__':
if __name__ == "__main__":
handle_run_main(main, server_address)
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.

0 comments on commit 62b76f7

Please sign in to comment.