/
temperaturetest.py
191 lines (150 loc) · 4.9 KB
/
temperaturetest.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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
#!/usr/bin/python
# Implemented by James YS Kim
import time
import socket
import sys
import getopt
import threading
import thread
from TCPClient import TCPClient
from time import gmtime, strftime, localtime
class MyThread(threading.Thread):
def __init__(self, serverip, serverport):
threading.Thread.__init__(self)
self.serverip = serverip
self.serverport = serverport
self.f = 0
self.timer1 = None
self.istimeout = 0
self.totaltrycount = 0
self.successcount = 0
self.failcount = 0
def stop(self):
sys.stdout.write('thread for %r ' % self.serverip )
sys.stdout.write('is shutdowning\r\n')
if not self.f.closed:
self.f.write("======================================\r\n")
self.f.write('[%r] stopped at %r\r\n' % (self.serverip, strftime("%d %b %Y %H:%M:%S", localtime())))
self.f.write("Total try: %r\r\n" % self.totaltrycount)
self.f.write("Success count: %r\r\n" % self.successcount)
self.f.write("Fail count: %r\r\n" % self.failcount)
self.f.write("======================================\r\n")
self.f.close()
self._Thread__stop()
def myTimer(self):
sys.stdout.write('timer1 timeout\r\n')
self.istimeout = 1
def run(self):
SOCK_CLOSE_STATE = 1
SOCK_OPENTRY_STATE = 2
SOCK_OPEN_STATE = 3
SOCK_CONNECTTRY_STATE = 4
SOCK_CONNECT_STATE = 5
idle_state = 1
datasent_state = 2
sys.stdout.write('thread for %r ' % self.serverip )
sys.stdout.write('is starting\r\n')
# TCPClient instance creation
client = TCPClient(2, self.serverip, self.serverport)
filename = self.serverip + '_log.txt'
#print(filename)
IsTimeout = 0
self.f = open(filename, 'w+')
while True:
if client.state is SOCK_CLOSE_STATE:
cur_state = client.state
client.state = client.open()
if client.state != cur_state:
sys.stdout.write('client.state is %r\r\n' % client.state)
time.sleep(1)
elif client.state is SOCK_OPEN_STATE:
cur_state = client.state
client.state = client.connect()
if client.state != cur_state:
sys.stdout.write('client.state is %r\r\n' % client.state)
time.sleep(1)
elif client.state is SOCK_CONNECT_STATE:
if client.working_state == idle_state:
try:
client.write(msg)
client.working_state = datasent_state
self.istimeout = 0
self.totaltrycount += 1
self.timer1 = threading.Timer(5.0, self.myTimer)
self.timer1.start()
except Exception as e:
sys.stdout.write('%r\r\n' % e)
elif client.working_state == datasent_state:
response = client.readline()
if(response != ""):
sys.stdout.write(response)
sys.stdout.write('\n')
sys.stdout.flush()
self.timer1.cancel()
self.istimeout = 0
if (msg in response) :
logstr = '[' + self.serverip + ']' + strftime("%d %b %Y %H:%M:%S", localtime()) + ': success\r\n'
sys.stdout.write(logstr)
self.successcount += 1
time.sleep(10)
client.working_state = idle_state
else :
logstr = '[' + self.serverip + ']' + strftime("%d %b %Y %H:%M:%S", localtime()) + ': fail by broken data\r\n'
sys.stdout.write(logstr)
self.failcount += 1
self.f.write(logstr)
self.f.write("\r\n")
time.sleep(10)
client.working_state = idle_state
if self.istimeout is 1:
self.timer1.cancel()
self.istimeout = 0
logstr = '[' + self.serverip + ']' + strftime("%d %b %Y %H:%M:%S", localtime()) + ': fail by timeout\r\n'
sys.stdout.write(logstr)
self.failcount += 1
self.f.write(logstr)
self.f.write("\r\n")
time.sleep(5)
client.working_state = idle_state
response = ""
if __name__=='__main__':
dst_ip = ''
dst_port = 5000
dst_num = 0
msg = "Hello WIZ750SR\r"
if len(sys.argv) <= 4:
sys.stdout.write('Invalid syntax. Refer to below\r\n')
sys.stdout.write('TempTest.py -s <WIZ107SR ip address> -c <server count>\r\n)')
sys.exit(0)
try:
opts, args = getopt.getopt(sys.argv[1:], "h:s:c:")
except getopt.GetoptError:
sys.stdout.write('Invalid syntax. Refer to below\r\n')
sys.stdout.write('TempTest.py -s <WIZ107SR ip address> -c <server count>\r\n)')
sys.exit(0)
sys.stdout.write('%r\r\n' % opts)
for opt, arg in opts:
if opt == '-h':
sys.stdout.write('Valid syntax\r\n')
sys.stdout.write('TempTest.py -s <WIZ107SR ip address> -c <server count>\r\n')
sys.exit(0)
elif opt in ("-s", "--sip") :
dst_ip = arg
sys.stdout.write('%r\r\n' % dst_ip)
elif opt in ("-c", "--count") :
dst_num = int(arg)
sys.stdout.write('%r\r\n' % dst_num)
lastnumindex = dst_ip.rfind('.')
lastnum = int(dst_ip[lastnumindex+1:len(dst_ip)])
threads = []
try:
for i in range(dst_num):
t = MyThread(dst_ip[:lastnumindex+1] + str(lastnum + i), dst_port)
threads.append(t)
for i in range(dst_num):
threads[i].start()
while True:
pass
except (KeyboardInterrupt, SystemExit):
for i in range(dst_num):
threads[i].stop()