From 0c848264084aa71751982418546e773777975a1d Mon Sep 17 00:00:00 2001 From: sp9wpn Date: Sat, 21 Sep 2024 22:51:56 +0200 Subject: [PATCH] support for Windows, added #quit command --- udp_client_console.py | 72 ++++++++++++++++++++++++++----------------- 1 file changed, 44 insertions(+), 28 deletions(-) diff --git a/udp_client_console.py b/udp_client_console.py index 076389c..868583a 100755 --- a/udp_client_console.py +++ b/udp_client_console.py @@ -5,15 +5,14 @@ import time import logging from mopp import * -import config import argparse -import sys -import select +import threading +import os logging.basicConfig(level=logging.DEBUG, format='%(message)s', ) argparser = argparse.ArgumentParser(description='MOPP - IP/UDP console client') -argparser.add_argument('ip', help='Server IP address') +argparser.add_argument('server', help='Server IP or hostname') argparser.add_argument('port', help='Server UDP port (default: 7373)', nargs='?', type=int, default=7373) args=argparser.parse_args() @@ -27,16 +26,55 @@ def transmit (data): if len(data) > 0: - sock.sendto(data, (args.ip, args.port)) + sock.sendto(data, (socket.gethostbyname(args.server), args.port)) + + +def inputThread(): + global speed + + while True: + i = input().strip() + if i == '#quit': + transmit(mopp.mopp(speed, ':bye')) + print("Quitting...") + os._exit(0) + break + + if i[0:1] == '#': + try: + _speed = int(i[1:]) + if (_speed >= 5 and _speed <= 60): + speed = _speed + print("Speed set to %d wpm." % speed) + else: + print("Allowed speeds from 5 to 60 wpm.") + + except: + pass + + continue + + for word in i.split(' '): + if word != '': + # print("Transmitting (%d wpm): %s" % (speed,word)) + transmit(mopp.mopp(speed, word)) + + print("### MOPP - IP/UDP console client") print("### Speed is set to %d wpm. To change, type: #" % speed) +print("### To exit the program, use #quit") print("") # Login with hi transmit(mopp.mopp(speed, 'hi')) +iThread = threading.Thread(target=inputThread) +iThread.daemon = True +iThread.start() + + while KeyboardInterrupt: try: data_bytes, addr = sock.recvfrom(64) @@ -49,33 +87,11 @@ def transmit (data): pass except (KeyboardInterrupt, SystemExit): + transmit(mopp.mopp(speed, ':bye')) sock.close() break pass except socket.timeout: - # time.sleep(0.2) # anti flood pass - _i, _o, _e = select.select([sys.stdin], [], [], 1) - if (_i): - input = sys.stdin.readline().strip() - - if input[0:1] == '#': - try: - _speed = int(input[1:]) - if (_speed >= 5 and _speed <= 60): - speed = _speed - print("Speed set to %d wpm." % speed) - else: - print("Allowed speeds from 5 to 60 wpm.") - - except: - pass - - continue - - for word in input.split(' '): - if word != '': - # print("Transmitting (%d wpm): %s" % (speed,word)) - transmit(mopp.mopp(speed, word))