mirror of
https://gitlab.vegova.si/rkv/flex-discovery-proxy.git
synced 2025-05-15 16:20:32 +00:00
Preoblikovan client v knjižnjico
client.py se sedaj lahko uporablja kot samostojen program (enaka uporaba kot prej) ali kot knjižnjica.
This commit is contained in:
parent
7963237bc0
commit
3e0311ba9b
1
.gitignore
vendored
Normal file
1
.gitignore
vendored
Normal file
@ -0,0 +1 @@
|
||||
/__pycache__
|
126
client.py
126
client.py
@ -9,6 +9,7 @@
|
||||
#
|
||||
# Author: Jakob Kordež, S52KJ
|
||||
# Docker: Žiga Kralj, S50ZK
|
||||
# GUI: Žiga Kralj, S50ZK
|
||||
#
|
||||
######################################################
|
||||
|
||||
@ -22,76 +23,77 @@ from socket import (
|
||||
SO_BROADCAST,
|
||||
)
|
||||
from select import select
|
||||
import sys
|
||||
from argparse import ArgumentParser
|
||||
import re
|
||||
|
||||
SERVER = "localhost"
|
||||
PORT = 4996
|
||||
def connect_proxy(server, port):
|
||||
# Create broadcast socket
|
||||
broadcastSocket = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP)
|
||||
broadcastSocket.setsockopt(SOL_SOCKET, SO_BROADCAST, 1)
|
||||
|
||||
# Connect to TCP server
|
||||
with socket(AF_INET, SOCK_STREAM) as s:
|
||||
s.settimeout(5)
|
||||
print("Connecting to server...", (server, port))
|
||||
|
||||
# Try to connect to the server for 5 seconds
|
||||
try:
|
||||
s.connect((server, port))
|
||||
print("Connected to server")
|
||||
print("\nPress Ctrl+C to exit...")
|
||||
except (ConnectionRefusedError, TimeoutError):
|
||||
print("Connection timed out")
|
||||
return 1
|
||||
|
||||
# Argument parsing
|
||||
serverRegex = r"^(?P<host>[^:]+)(:(?P<port>\d+))?$"
|
||||
s.setblocking(False)
|
||||
|
||||
parser = ArgumentParser(
|
||||
prog="FlexRadio discovery proxy client",
|
||||
description="Connect to a FlexRadio discovery proxy and re-broadcast all received packets",
|
||||
epilog="Author: Jakob Kordež, S52KJ",
|
||||
)
|
||||
parser.add_argument(
|
||||
"server",
|
||||
metavar="SERVER",
|
||||
type=str,
|
||||
nargs="?",
|
||||
default=SERVER,
|
||||
help="Server address (default: %(default)s)",
|
||||
)
|
||||
try:
|
||||
while True:
|
||||
r = select([s], [], [], 1)[0]
|
||||
if len(r) == 0:
|
||||
continue
|
||||
|
||||
args = parser.parse_args()
|
||||
sp = re.fullmatch(serverRegex, args.server)
|
||||
if not sp:
|
||||
print("Invalid server address")
|
||||
sys.exit(1)
|
||||
payload = s.recv(2048)
|
||||
if not payload:
|
||||
print("Server disconnected")
|
||||
break
|
||||
|
||||
SERVER = sp.group("host")
|
||||
if sp.group("port"):
|
||||
PORT = int(sp.group("port"))
|
||||
broadcastSocket.sendto(payload, ("255.255.255.255", 4992))
|
||||
except KeyboardInterrupt:
|
||||
print("\nExiting...")
|
||||
return 0
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
import sys
|
||||
from argparse import ArgumentParser
|
||||
import re
|
||||
SERVER = "localhost"
|
||||
PORT = 4996
|
||||
|
||||
# Argument parsing
|
||||
serverRegex = r"^(?P<host>[^:]+)(:(?P<port>\d+))?$"
|
||||
|
||||
# Create broadcast socket
|
||||
broadcastSocket = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP)
|
||||
broadcastSocket.setsockopt(SOL_SOCKET, SO_BROADCAST, 1)
|
||||
parser = ArgumentParser(
|
||||
prog="FlexRadio discovery proxy client",
|
||||
description="Connect to a FlexRadio discovery proxy and re-broadcast all received packets",
|
||||
epilog="Author: Jakob Kordež, S52KJ",
|
||||
)
|
||||
parser.add_argument(
|
||||
"server",
|
||||
metavar="SERVER",
|
||||
type=str,
|
||||
nargs="?",
|
||||
default=SERVER,
|
||||
help="Server address (default: %(default)s)",
|
||||
)
|
||||
|
||||
|
||||
# Connect to TCP server
|
||||
with socket(AF_INET, SOCK_STREAM) as s:
|
||||
s.settimeout(5)
|
||||
print("Connecting to server...", (SERVER, PORT))
|
||||
|
||||
# Try to connect to the server for 5 seconds
|
||||
try:
|
||||
s.connect((SERVER, PORT))
|
||||
print("Connected to server")
|
||||
print("\nPress Ctrl+C to exit...")
|
||||
except (ConnectionRefusedError, TimeoutError):
|
||||
print("Connection timed out")
|
||||
input("\nPress enter to exit...")
|
||||
args = parser.parse_args()
|
||||
sp = re.fullmatch(serverRegex, args.server)
|
||||
if not sp:
|
||||
print("Invalid server address")
|
||||
sys.exit(1)
|
||||
|
||||
s.setblocking(False)
|
||||
|
||||
try:
|
||||
while True:
|
||||
r = select([s], [], [], 1)[0]
|
||||
if len(r) == 0:
|
||||
continue
|
||||
|
||||
payload = s.recv(2048)
|
||||
if not payload:
|
||||
print("Server disconnected")
|
||||
break
|
||||
|
||||
broadcastSocket.sendto(payload, ("255.255.255.255", 4992))
|
||||
except KeyboardInterrupt:
|
||||
print("\nExiting...")
|
||||
sys.exit(0)
|
||||
SERVER = sp.group("host")
|
||||
if sp.group("port"):
|
||||
PORT = int(sp.group("port"))
|
||||
|
||||
sys.exit(connect_proxy(SERVER, PORT))
|
||||
|
Loading…
x
Reference in New Issue
Block a user