-
Notifications
You must be signed in to change notification settings - Fork 83
/
Copy pathvalidation.py
68 lines (55 loc) · 1.83 KB
/
validation.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# Written by @https://github.com/SerpentBTW
"""
All functions here CAN use print() because they are not to be called during
gameplay. They are only to be called during the setup of the game, where
printing directly using print() is not an issue. Do not use in any main()
functions or modules that are called during gameplay.
"""
def validate_name(name) -> bool:
"""
names cannot be greater than 8 characters
alphnumeric, spaces, and apostrophes are allowed
"""
if len(name) > 8:
return False
for char in name:
if not (char.isalnum() or char == " " or char == "'"):
return False
return True
import subprocess
def is_port_unused(port: int) -> bool:
"""
Check if port is being used in any capacity. If the port inputted is, the program will return False.
"""
result = subprocess.run(["netstat", "-an"], capture_output=True, text=True)
if f":{port}" in result.stdout:
print(f"Port {port} is already in use.")
return False
return True
def validate_port(port: str):
"""
Validate a port. The port must be a number between 1024 and 65535.
"""
if not port.isdigit():
print(f"Port {port} is not a number.")
return False
port = int(port)
if port < 1024 or port > 65535:
print(f"Port {port} is not in the valid range of 1024-65535.")
return False
return True
def validate_address(address: str) -> bool:
"""
Validate an IP address. The address must be in the form of 4 numbers separated by dots.
Each number must be between 0 and 255.
"""
if address.count(".") != 3:
return False
parts = address.split(".")
for part in parts:
if not part.isdigit():
return False
part = int(part)
if part < 0 or part > 255:
return False
return True