@@ -35,11 +35,24 @@ def execute(shell, cmd):
35
35
sw = splitted [1 ].lower ()
36
36
if sw == "on" :
37
37
if not shell .rest_thread :
38
+ import socket
39
+ s = socket .socket (socket .AF_INET , socket .SOCK_STREAM )
40
+ try :
41
+ s .bind (('127.0.0.1' , int (port )))
42
+ except OSError as e :
43
+ if e .errno == 98 :
44
+ shell .print_error ("Port %s is already bound!" % (port ))
45
+ elif e .errno == 13 :
46
+ shell .print_error ("Port %s bind permission denied!" % (port ))
47
+ s .close ()
48
+ return
49
+ s .close ()
50
+
38
51
rest_server = core .rest_server .RestServer (shell , port , username , password )
39
52
def thread_rest_server ():
40
53
try :
41
54
rest_server .run ()
42
- except SystemExit as e :
55
+ except SystemExit :
43
56
pass
44
57
45
58
stdout = sys .stdout
@@ -50,13 +63,18 @@ def thread_rest_server():
50
63
shell .rest_thread .start ()
51
64
time .sleep (1 )
52
65
sys .stdout = stdout
53
- if shell .rest_thread :
66
+ # ok, now THIS is the most embarassing thing i've ever done.
67
+ # i don't know how to pass exceptions from the thread to the caller.
68
+ # so here we are.
69
+ if "started" in shell .rest_thread .localtrace (0 ,0 ,0 ).__str__ ():
54
70
shell .print_good ("Rest server running on port %s" % port )
55
71
shell .print_status ("Username: %s" % username )
56
72
shell .print_status ("Password: %s" % password )
57
73
shell .print_status ("API Token: %s" % rest_server .token )
58
74
else :
59
- shell .print_error ("Could not start rest server: " )
75
+ shell .rest_thread .kill ()
76
+ shell .rest_thread = ""
77
+ shell .print_error ("Could not start rest server." )
60
78
61
79
else :
62
80
shell .print_error ("Rest server already running" )
@@ -67,3 +85,6 @@ def thread_rest_server():
67
85
shell .print_good ("Rest server shutdown" )
68
86
else :
69
87
shell .print_error ("Rest server not running" )
88
+
89
+ else :
90
+ help (shell )
0 commit comments