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
if __name__ == '__main__':
asyncio.run(
cmd_chat.run()
)
asyncio.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?
* The client generates a private key.
@ -40,3 +68,8 @@ How does encryption work?
# Example
![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 argparse
from cmd_chat.server.server import app
from cmd_chat.client.client import Client
@ -28,23 +29,36 @@ async def run_client(
async def run() -> None:
action: int = int(
input("Choose action:\n1. Run server\n2. Run client\nAction: ")
parser = argparse.ArgumentParser(
description='Command-line chat application'
)
if action == 1:
await run_server(
input("IP: "),
int(input("PORT: "))
)
if action == 2:
await run_client(
input("USERNAME: "),
input("IP: "),
int(input("PORT: "))
)
parser.add_argument(
'command',
choices=['serve', 'connect'],
help='Command to execute'
)
parser.add_argument(
'ip_address',
help='IP address to serve or connect'
)
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__':
asyncio.run(
run()
)
asyncio.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.config import (
COLORS
COLORS,
RENDER_TIME
)
@ -109,13 +110,13 @@ class Client(RSAService):
def update_info(self):
""" connecting to websocket,
wating for updates,
updating every 0.05 seconds
updating every RENDER_TIME seconds
"""
ws = create_connection(f"{self.ws_url}/update")
last_try = None
while True:
try:
time.sleep(0.05)
time.sleep(RENDER_TIME)
response = ast.literal_eval(ws.recv().decode('utf-8'))
if last_try == response:
continue

View File

@ -5,4 +5,6 @@ COLORS = {
"my_username_color": Fore.MAGENTA,
"ip_color": Fore.MAGENTA,
"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(
ws: Websocket
) -> dict:
ws_data = await ws.recv()
return ast.literal_eval(ws_data.decode('utf-8'))
return ast.literal_eval(await ws.recv())
async def _check_ws_for_close_status(