KERNEL: fix set demo for module with demo data
[odoo/odoo.git] / bin / tiny_socket.py
1 import socket
2 import cPickle
3 import marshal
4
5 class Myexception(Exception):
6         def __init__(self, faultCode, faultString):
7                 self.faultCode = faultCode
8                 self.faultString = faultString
9
10 class mysocket:
11         def __init__(self, sock=None):
12                 if sock is None:
13                         self.sock = socket.socket(
14                         socket.AF_INET, socket.SOCK_STREAM)
15                 else:
16                         self.sock = sock
17                 self.sock.settimeout(60)
18         def connect(self, host, port=False):
19                 if not port:
20                         protocol, buf = host.split('//')
21                         host, port = buf.split(':')
22                 self.sock.connect((host, int(port)))
23         def disconnect(self):
24                 self.sock.shutdown(socket.SHUT_RDWR)
25                 self.sock.close()
26         def mysend(self, msg, exception=False):
27                 msg = cPickle.dumps(msg)
28                 size = len(msg)
29                 self.sock.send('%8d' % size)
30                 self.sock.send(exception and "1" or "0")
31                 totalsent = 0
32                 while totalsent < size:
33                         sent = self.sock.send(msg[totalsent:])
34                         if sent == 0:
35                                 raise RuntimeError, "socket connection broken"
36                         totalsent = totalsent + sent
37         def myreceive(self):
38                 buf=''
39                 while len(buf) < 8:
40                         chunk = self.sock.recv(8 - len(buf))
41                         if chunk == '':
42                                 raise RuntimeError, "socket connection broken"
43                         buf += chunk
44                 size = int(buf)
45                 buf = self.sock.recv(1)
46                 if buf != "0":
47                         exception = buf
48                 else:
49                         exception = False
50                 msg = ''
51                 while len(msg) < size:
52                         chunk = self.sock.recv(size-len(msg))
53                         if chunk == '':
54                                 raise RuntimeError, "socket connection broken"
55                         msg = msg + chunk
56                 res = cPickle.loads(msg)
57                 if isinstance(res,Exception):
58                         if exception:
59                                 raise Myexception(exception, str(res))
60                         raise res
61                 else:
62                         return res