-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsocketClient.py
More file actions
executable file
·94 lines (78 loc) · 2.41 KB
/
socketClient.py
File metadata and controls
executable file
·94 lines (78 loc) · 2.41 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
91
92
93
94
#!/usr/bin/env python
# encoding: UTF-8
import socket
import sys,getopt
import xmlReader
import CondorTools
chirp = CondorTools.CondorChirp()
class Client:
def __init__(self,config):
self.config = config
print ("client init")
def get_constant(self, prefix):
"""Create a dictionary mapping socket module constants to their names."""
return dict( (getattr(socket, n), n)
for n in dir(socket) if n.startswith(prefix)
)
def get_constants(self, sock):
families = self.get_constant('AF_')
types = self.get_constant('SOCK_')
protocols = self.get_constant('IPPROTO_')
print >>sys.stderr, 'Family :', families[sock.family]
print >>sys.stderr, 'Type :', types[sock.type]
print >>sys.stderr, 'Protocol:', protocols[sock.proto]
print >>sys.stderr
def demand(self):
print ("client demand")
try:
xmlHandler = xmlReader.XmlHandler(self.config)
except:
print "xml read error"
sys.exit()
host, port, path, timestamp, offset = xmlHandler.read()
print "five vals:"
print host, port, path, timestamp, offset
interval = 5
maxtries = 12*3
serverInfo = chirp.getJobAttrWait("SocketServer",None,interval, maxtries)
print "serverInfo is:" + serverInfo
#hostFromCondor,portFromCondor = serverInfo.strip("'").split()
#print hostFromCondor, portFromCondor
# Create a TCP/IP socket
sock = socket.create_connection((host, int(port)))
self.get_constants(sock)
print host, port, path, timestamp, offset
try:
# Send data
message = timestamp + ',' + offset
sock.sendall(message)
amount_received = 0
amount = int(sock.recv(8))
#print amount
sock.sendall("kunBegin")
dataComp = ""
while amount_received < int(amount):
print (amount_received, amount)
data = sock.recv(min(4096, int(amount) - amount_received))
dataComp += data
amount_received += len(data)
strAdded, timestamp, offset = dataComp.split("KUNSIGN")
print strAdded
if not amount_received < amount:
with open(path, "a") as output:
output.write(strAdded)
print "time is " + timestamp
if timestamp and offset:
xmlHandler.write(timestamp, offset, self.config)
except:
sock.sendall("kunStop")
print "amount value error"
finally:
print >>sys.stderr, 'closing socket'
sock.close()
print("dadada")
if __name__ == '__main__':
if len(sys.argv) < 2:
print ("client val num error!")
client = Client(sys.argv[1])
client.demand()