mirror of
https://gitlab.vegova.si/rkv/flex-discovery-proxy.git
synced 2025-07-12 08:57:41 +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:
1
.gitignore
vendored
Normal file
1
.gitignore
vendored
Normal file
@ -0,0 +1 @@
|
|||||||
|
/__pycache__
|
82
client.py
82
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,14 +23,52 @@ from socket import (
|
|||||||
SO_BROADCAST,
|
SO_BROADCAST,
|
||||||
)
|
)
|
||||||
from select import select
|
from select import select
|
||||||
|
|
||||||
|
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
|
||||||
|
|
||||||
|
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...")
|
||||||
|
return 0
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
import sys
|
import sys
|
||||||
from argparse import ArgumentParser
|
from argparse import ArgumentParser
|
||||||
import re
|
import re
|
||||||
|
|
||||||
SERVER = "localhost"
|
SERVER = "localhost"
|
||||||
PORT = 4996
|
PORT = 4996
|
||||||
|
|
||||||
|
|
||||||
# Argument parsing
|
# Argument parsing
|
||||||
serverRegex = r"^(?P<host>[^:]+)(:(?P<port>\d+))?$"
|
serverRegex = r"^(?P<host>[^:]+)(:(?P<port>\d+))?$"
|
||||||
|
|
||||||
@ -57,41 +96,4 @@ SERVER = sp.group("host")
|
|||||||
if sp.group("port"):
|
if sp.group("port"):
|
||||||
PORT = int(sp.group("port"))
|
PORT = int(sp.group("port"))
|
||||||
|
|
||||||
|
sys.exit(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")
|
|
||||||
input("\nPress enter to exit...")
|
|
||||||
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)
|
|
||||||
|
Reference in New Issue
Block a user