Add CLI run options, update README

This commit is contained in:
mirai 2023-11-27 06:50:16 +03:00
parent 8c4799c634
commit c5fa982f65
7 changed files with 76 additions and 25 deletions

View File

@ -23,11 +23,39 @@ import asyncio
import cmd_chat import cmd_chat
if __name__ == '__main__': if __name__ == '__main__':
asyncio.run( asyncio.run(cmd_chat.run())
cmd_chat.run()
)
``` ```
### Or (Windows)
Start server:
```
.\cmd_chat.bat serve localhost 5000
```
Connect to server:
```
.\cmd_chat.bat connect localhost 5000 tyler
```
### Or (Linux)
Start server:
```
python3 cmd_chat.py serve localhost 5000
```
Connect to server:
```
python3 cmd_chat.py connect localhost 5000 tyler
```
How does encryption work? How does encryption work?
* The client generates a private key. * The client generates a private key.
@ -40,3 +68,8 @@ How does encryption work?
# Example # Example
![Alt Text](example.gif) ![Alt Text](example.gif)
# Known bugs
* Sometime WS just drop connection
* Client input message problem. To start input, you need to press enter first, only after that you got pop up with message. Tried to fix, but nothing worked.

2
cmd_chat.bat Normal file
View File

@ -0,0 +1,2 @@
@echo off
python cmd_chat.py %*

View File

@ -1,4 +1,5 @@
import asyncio import asyncio
import argparse
from cmd_chat.server.server import app from cmd_chat.server.server import app
from cmd_chat.client.client import Client from cmd_chat.client.client import Client
@ -28,23 +29,36 @@ async def run_client(
async def run() -> None: async def run() -> None:
action: int = int( parser = argparse.ArgumentParser(
input("Choose action:\n1. Run server\n2. Run client\nAction: ") description='Command-line chat application'
) )
if action == 1: parser.add_argument(
await run_server( 'command',
input("IP: "), choices=['serve', 'connect'],
int(input("PORT: ")) help='Command to execute'
) )
if action == 2: parser.add_argument(
await run_client( 'ip_address',
input("USERNAME: "), help='IP address to serve or connect'
input("IP: "),
int(input("PORT: "))
) )
parser.add_argument(
'port',
help='PORT of server'
)
parser.add_argument(
'username',
nargs='?',
default='',
help='Username for connection (required for connect command)'
)
args = parser.parse_args()
if args.command == 'serve':
await run_server(args.ip_address, int(args.port))
elif args.command == 'connect':
if not args.username:
parser.error("Username is required for 'connect' command")
await run_client(args.username, args.ip_address, int(args.port))
if __name__ == '__main__': if __name__ == '__main__':
asyncio.run( asyncio.run(run())
run()
)

View File

@ -9,7 +9,8 @@ from websocket import create_connection
from cmd_chat.client.core.crypto import RSAService from cmd_chat.client.core.crypto import RSAService
from cmd_chat.client.config import ( from cmd_chat.client.config import (
COLORS COLORS,
RENDER_TIME
) )
@ -109,13 +110,13 @@ class Client(RSAService):
def update_info(self): def update_info(self):
""" connecting to websocket, """ connecting to websocket,
wating for updates, wating for updates,
updating every 0.05 seconds updating every RENDER_TIME seconds
""" """
ws = create_connection(f"{self.ws_url}/update") ws = create_connection(f"{self.ws_url}/update")
last_try = None last_try = None
while True: while True:
try: try:
time.sleep(0.05) time.sleep(RENDER_TIME)
response = ast.literal_eval(ws.recv().decode('utf-8')) response = ast.literal_eval(ws.recv().decode('utf-8'))
if last_try == response: if last_try == response:
continue continue

View File

@ -6,3 +6,5 @@ COLORS = {
"ip_color": Fore.MAGENTA, "ip_color": Fore.MAGENTA,
"username_color": Fore.GREEN "username_color": Fore.GREEN
} }
RENDER_TIME = 0.05

View File

@ -6,8 +6,7 @@ from cmd_chat.server.models import Message
async def _get_bytes_and_serialize( async def _get_bytes_and_serialize(
ws: Websocket ws: Websocket
) -> dict: ) -> dict:
ws_data = await ws.recv() return ast.literal_eval(await ws.recv())
return ast.literal_eval(ws_data.decode('utf-8'))
async def _check_ws_for_close_status( async def _check_ws_for_close_status(