mirror of
https://gitlab.vegova.si/rkv/flex-discovery-proxy.git
synced 2025-05-16 00:30:30 +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
|
# Author: Jakob Kordež, S52KJ
|
||||||
# Docker: Žiga Kralj, S50ZK
|
# Docker: Žiga Kralj, S50ZK
|
||||||
|
# GUI: Žiga Kralj, S50ZK
|
||||||
#
|
#
|
||||||
######################################################
|
######################################################
|
||||||
|
|
||||||
@ -22,76 +23,77 @@ from socket import (
|
|||||||
SO_BROADCAST,
|
SO_BROADCAST,
|
||||||
)
|
)
|
||||||
from select import select
|
from select import select
|
||||||
import sys
|
|
||||||
from argparse import ArgumentParser
|
|
||||||
import re
|
|
||||||
|
|
||||||
SERVER = "localhost"
|
def connect_proxy(server, port):
|
||||||
PORT = 4996
|
# 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
|
s.setblocking(False)
|
||||||
serverRegex = r"^(?P<host>[^:]+)(:(?P<port>\d+))?$"
|
|
||||||
|
|
||||||
parser = ArgumentParser(
|
try:
|
||||||
prog="FlexRadio discovery proxy client",
|
while True:
|
||||||
description="Connect to a FlexRadio discovery proxy and re-broadcast all received packets",
|
r = select([s], [], [], 1)[0]
|
||||||
epilog="Author: Jakob Kordež, S52KJ",
|
if len(r) == 0:
|
||||||
)
|
continue
|
||||||
parser.add_argument(
|
|
||||||
"server",
|
|
||||||
metavar="SERVER",
|
|
||||||
type=str,
|
|
||||||
nargs="?",
|
|
||||||
default=SERVER,
|
|
||||||
help="Server address (default: %(default)s)",
|
|
||||||
)
|
|
||||||
|
|
||||||
args = parser.parse_args()
|
payload = s.recv(2048)
|
||||||
sp = re.fullmatch(serverRegex, args.server)
|
if not payload:
|
||||||
if not sp:
|
print("Server disconnected")
|
||||||
print("Invalid server address")
|
break
|
||||||
sys.exit(1)
|
|
||||||
|
|
||||||
SERVER = sp.group("host")
|
broadcastSocket.sendto(payload, ("255.255.255.255", 4992))
|
||||||
if sp.group("port"):
|
except KeyboardInterrupt:
|
||||||
PORT = int(sp.group("port"))
|
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
|
parser = ArgumentParser(
|
||||||
broadcastSocket = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP)
|
prog="FlexRadio discovery proxy client",
|
||||||
broadcastSocket.setsockopt(SOL_SOCKET, SO_BROADCAST, 1)
|
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)",
|
||||||
|
)
|
||||||
|
|
||||||
|
args = parser.parse_args()
|
||||||
# Connect to TCP server
|
sp = re.fullmatch(serverRegex, args.server)
|
||||||
with socket(AF_INET, SOCK_STREAM) as s:
|
if not sp:
|
||||||
s.settimeout(5)
|
print("Invalid server address")
|
||||||
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...")
|
|
||||||
sys.exit(1)
|
sys.exit(1)
|
||||||
|
|
||||||
s.setblocking(False)
|
SERVER = sp.group("host")
|
||||||
|
if sp.group("port"):
|
||||||
try:
|
PORT = int(sp.group("port"))
|
||||||
while True:
|
|
||||||
r = select([s], [], [], 1)[0]
|
sys.exit(connect_proxy(SERVER, PORT))
|
||||||
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)
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user