Terarea
2
The automation project
Loading...
Searching...
No Matches
server_main.py
Go to the documentation of this file.
1
"""
2
The file allowing the server to be run as a standalone.
3
"""
4
5
import
sys
6
from
sys
import
argv
7
8
try
:
9
from
.lib
import
Server, CONST
10
except
ImportError:
11
from
lib
import
Server, CONST
12
13
14
class
Main
:
15
"""_summary_
16
This class is a bootstrapper for launching the server in standalone mode.
17
"""
18
19
def
__init__
(self, success: int = 0, error: int = 84) ->
None
:
20
self.
argc
= len(argv)
21
self.
host
host
: str =
"0.0.0.0"
22
self.
port
: int = 5000
23
self.
success
: int = success
24
self.
error
: int = error
25
self.
app_name
: str =
"Area"
26
self.
debug
: bool =
False
27
28
def
process_args
(self) -> None:
29
"""_summary_
30
Check the arguments that are input (if any)
31
"""
32
i = 1
33
while
i < self.
argc
:
34
arg = argv[i].lower()
35
if
"--help"
in
arg
or
"-h"
== arg:
36
print(
"Usage: python3 ./server [OPTIONS]"
)
37
print(
"Options:"
)
38
print(
39
" --host=<host> The host to bind the server to (default: '0.0.0.0')"
40
)
41
print(
42
" --port=<port>, -p <port> The port to bind the server to (default: 5000)"
43
)
44
print(
45
" --success=<number>, -s <number> Change the success exit code (default: 0)"
46
)
47
print(
48
" --error=<number>, -e <number> Change the error exit code (default: 1)"
49
)
50
print(
51
" --debug Enable debug mode"
52
)
53
print(
54
" --help, -h Show this help message"
55
)
56
sys.exit(self.
success
)
57
elif
"--host"
in
arg:
58
if
'='
in
arg:
59
self.
host
host
= arg.split(
"="
)[1]
60
else
:
61
self.
host
host
= argv[i + 1]
62
i += 1
63
elif
"--port"
in
arg
or
'-p'
in
arg:
64
if
'='
in
arg:
65
self.
port
= int(arg.split(
"="
)[1])
66
else
:
67
self.
port
= int(argv[i + 1])
68
i += 1
69
elif
"--success"
in
arg
or
"-s"
in
arg:
70
if
'='
in
arg:
71
self.
success
= int(arg.split(
"="
)[1])
72
else
:
73
self.
success
= int(argv[i + 1])
74
i += 1
75
elif
"--error"
in
arg
or
"-e"
in
arg:
76
if
'='
in
arg:
77
self.
error
= int(arg.split(
"="
)[1])
78
else
:
79
self.
error
= int(argv[i + 1])
80
i += 1
81
elif
"--debug"
in
arg
or
"-d"
in
arg:
82
self.
debug
=
True
83
84
else
:
85
print(f
"Unknown argument: {arg}"
)
86
i += 1
87
88
def
main
(self) -> None:
89
"""_summary_
90
This method is the entry point of the server.
91
"""
92
if
self.
argc
> 1:
93
self.
process_args
()
94
SI =
Server
(
95
host=self.
host
host
,
96
port=self.
port
,
97
success=self.
success
,
98
error=self.
error
,
99
app_name=self.
app_name
,
100
debug=self.
debug
101
)
102
try
:
103
status = SI.main()
104
except
KeyboardInterrupt:
105
print(
"\nCtrl+C caught! Exiting the program gracefully."
)
106
del SI
107
status = self.
success
108
except
Exception
as
e:
109
print(f
"An error occurred: {e}"
)
110
status = self.
error
111
print(f
"The server is exiting with a status of: {status}"
)
112
sys.exit(status)
113
114
else
:
115
print(
"Usage: python3 ./server --help"
)
116
sys.exit(self.
success
)
117
118
119
if
__name__ ==
"__main__"
:
120
MI =
Main
(
121
success=CONST.SUCCESS,
122
error=CONST.ERROR
123
)
124
MI.main()
src.lib.server.Server
Definition
server.py:16
src.server_main.Main
Definition
server_main.py:14
src.server_main.Main.process_args
None process_args(self)
Definition
server_main.py:28
src.server_main.Main.success
int success
Definition
server_main.py:23
src.server_main.Main.main
None main(self)
Definition
server_main.py:88
src.server_main.Main.port
int port
Definition
server_main.py:22
src.server_main.Main.host
int host
Definition
server_main.py:61
src.server_main.Main.debug
bool debug
Definition
server_main.py:26
src.server_main.Main.error
int error
Definition
server_main.py:24
src.server_main.Main.__init__
None __init__(self, int success=0, int error=84)
Definition
server_main.py:19
src.server_main.Main.app_name
str app_name
Definition
server_main.py:25
src.server_main.Main.argc
argc
Definition
server_main.py:20
src.server_main.Main.host
str host
Definition
server_main.py:21
app
back
server
src
server_main.py
Generated on Sat Feb 22 2025 14:36:42 for Terarea by
1.12.0