1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 """
23 Class of ETL socket.
24 """
25 import socket
26 import cPickle
27 import marshal
28
30 - def __init__(self, faultCode, faultString):
31 self.faultCode = faultCode
32 self.faultString = faultString
33 self.args = (faultCode, faultString)
34
35 DNS_CACHE = {}
38 if sock is None:
39 self.sock = socket.socket(
40 socket.AF_INET, socket.SOCK_STREAM)
41 else:
42 self.sock = sock
43 self.sock.settimeout(120)
44
46 if not port:
47 protocol, buf = host.split('//')
48 host, port = buf.split(':')
49 if host in DNS_CACHE:
50 host = DNS_CACHE[host]
51 self.sock.connect((host, int(port)))
52 DNS_CACHE[host], port = self.sock.getpeername()
53
57
58 - def mysend(self, msg, exception=False, traceback=None):
59 msg = cPickle.dumps([msg,traceback])
60 self.sock.sendall('%8d%s%s' % (len(msg), exception and "1" or "0", msg))
61
63 def read(socket, size):
64 buf = ''
65 while len(buf) < size:
66 chunk = self.sock.recv(size - len(buf))
67 if chunk == '':
68 raise RuntimeError, "socket connection broken"
69 buf += chunk
70 return buf
71
72 size = int(read(self.sock, 8))
73 buf = read(self.sock, 1)
74 exception = buf != '0' and buf or False
75 res = cPickle.loads(read(self.sock, size))
76
77 if isinstance(res[0],Exception):
78 if exception:
79 raise etl_socket_exception(str(res[0]), str(res[1]))
80 raise res[0]
81 else:
82 return res[0]
83