-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsocketServer.py
More file actions
executable file
·91 lines (77 loc) · 2.06 KB
/
socketServer.py
File metadata and controls
executable file
·91 lines (77 loc) · 2.06 KB
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
#!/usr/bin/env python
# encoding: UTF-8
import socket
import sys
import time
from thread import *
import fileReader
import re, getopt
import CondorTools
HOST = ""
PORT = 8888
chirp = CondorTools.CondorChirp()
class Server:
def __init__(self, path, host, port):
self.path = path
HOST = host
PORT = port
def commuWithClient(self, conn):
#conn.send("welcome!\n")
timestamp = ""
timestampNew = ""
offset = ""
offsetNew = ""
while True:
data = conn.recv(64)
print ("data is: " + data + " type is: " + str(type(data)))
if self.match(r"\d+(\.\d+)?,\d+", data):
timestamp, offset = data.split(',')
strAdded, timestampNew, offsetNew= fileReader.FileReader().chooseLines(float(timestamp), int(offset), self.path)
dataToSend = strAdded + "KUNSIGN" + timestampNew + "KUNSIGN" + offsetNew
print(len(strAdded))
conn.sendall(str(len(dataToSend)))
elif self.match("kunBegin", data):
conn.sendall(dataToSend)
elif self.match("kunStop", data):
#print("value error")
raise
else:
print("Done!")
break
#conn.send("bye\n")
#conn.close()
def match(self, reg, strToMatch):
return re.compile(reg).match(strToMatch)
def changeFlag(self):
time.sleep(2)
chirp.setJobAttr("SocketServer","'%s %d'" % (HOST, PORT))
print("ChangeFlag()")
def serve(self):
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
print ("Socket created")
try:
s.bind((HOST, PORT))
except socket.error, msg:
print ("Bind failed. Error Code : " + str(msg[0]) + " Message " + msg[1])
sys.exit()
print ("Socket bind complete")
s.listen(1)
print ("socket now listening")
flagOfCondor = ""
start_new_thread(self.changeFlag, ())
conn, addr = s.accept()
print ("Connected with " + addr[0] + ":" + str(addr[1]))
try:
self.commuWithClient(conn)
except :
print ("value error!")
finally:
s.close()
print ("socket close now !")
print("exit")
sys.exit()
if __name__ == '__main__':
if len(sys.argv) < 4:
print ("server val num error")
server = Server(sys.argv[1], sys.argv[2], sys.argv[3])
server.serve()