|
1 | 1 | import argparse |
| 2 | +import socket |
2 | 3 | from api import get_udp_socket, log, parse_raw_pkt, MOTD_PKT |
3 | 4 |
|
4 | 5 |
|
5 | | -def send_pkt(addr, port, timeout: float=3.0): |
6 | | - udp_skt = get_udp_socket() |
7 | | - udp_skt.settimeout(timeout) |
| 6 | +def send_pkt(addr, port, timeout: float=3.0, local_port: int=None): |
| 7 | + udp_skt = get_udp_socket(local_port, timeout) |
8 | 8 | udp_skt.sendto( |
9 | 9 | MOTD_PKT, |
10 | 10 | (addr, port)) |
@@ -36,16 +36,19 @@ def recv_pkt(sk_send): |
36 | 36 | help="target server port") |
37 | 37 | parser.add_argument("-t", "--timeout", default=3.0, type=float, |
38 | 38 | help="timeout") |
| 39 | + parser.add_argument("-lp", "--local-port", default=None, type=int, |
| 40 | + help="local port to send motd packet") |
39 | 41 |
|
40 | 42 | args = parser.parse_args() |
41 | 43 |
|
42 | 44 | addr = args.addr |
43 | 45 | port = args.port |
44 | 46 | timeout = args.timeout |
| 47 | + local_port = args.local_port |
45 | 48 |
|
46 | 49 | try: |
47 | | - send_pkt(addr, port, timeout) |
48 | | - except TimeoutError: |
| 50 | + send_pkt(addr, port, timeout, local_port) |
| 51 | + except (TimeoutError, socket.timeout): |
49 | 52 | log(f"Timeout! Server may be offline or blocked motd request.") |
50 | 53 | except ConnectionResetError: |
51 | 54 | log(f"Connection Reset! Server may be offline or blocked motd request.") |
0 commit comments