from server import server import sys from optparse import OptionParser

 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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
from server import server
import sys
from optparse import OptionParser
from ctypes import windll, c_int, c_char_p
import threading
ERROR_ALREADY_EXISTS = c_int(183)
EVENT_ALL_ACCESS = c_int(0x1F0003)
INFINITE = c_int(0xFFFFFFFF)
CFALSE = c_int(0)
CTRUE = c_int(1)
CreateEvent = windll.kernel32.CreateEventA
CloseHandle = windll.kernel32.CloseHandle
GetLastError = windll.kernel32.GetLastError
WaitForSingleObject = windll.kernel32.WaitForSingleObject
OpenEvent = windll.kernel32.OpenEventA
SetEvent = windll.kernel32.SetEvent
tester_event_name = c_char_p('PyTester')
class ServerThread(threading.Thread):
def __init__(self):
threading.Thread.__init__(self)
def run(self):
server.start()
def create_parser():
parser = OptionParser()
parser.add_option('--start', action='store_const',
const='start', dest='action')
parser.add_option('--stop', action='store_const',
const='stop', dest='action')
return parser
def server_start():
event = CreateEvent(None, CFALSE, CFALSE, tester_event_name)
if GetLastError() == ERROR_ALREADY_EXISTS:
CloseHandle(event)
return 1
st = ServerThread()
st.start()
print WaitForSingleObject(event, INFINITE)
print GetLastError()
server.stop()
CloseHandle(event)
return 0
def server_stop():
event = OpenEvent(EVENT_ALL_ACCESS, CFALSE, tester_event_name)
if event is None:
return 1
SetEvent(event)
CloseHandle(event)
return 0
def main():
parser = create_parser()
(options, args) = parser.parse_args(sys.argv[1:])
if options.action is None:
return 1
if options.action == 'start':
return server_start()
elif options.action == 'stop':
return server_stop()
else: return 1
return 0
if __name__ == '__main__':
sys.exit(main())