1 # -*- coding: utf-8 -*-
2 ##############################################################################
4 # OpenERP, Open Source Management Solution
5 # Copyright (C) 2004-2009 Tiny SPRL (<http://tiny.be>).
7 # This program is free software: you can redistribute it and/or modify
8 # it under the terms of the GNU Affero General Public License as
9 # published by the Free Software Foundation, either version 3 of the
10 # License, or (at your option) any later version.
12 # This program is distributed in the hope that it will be useful,
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 # GNU Affero General Public License for more details.
17 # You should have received a copy of the GNU Affero General Public License
18 # along with this program. If not, see <http://www.gnu.org/licenses/>.
20 ##############################################################################
29 class Myexception(Exception):
31 custome exception object store
37 def __init__(self, faultCode, faultString):
38 self.faultCode = faultCode
39 self.faultString = faultString
40 self.args = (faultCode, faultString)
44 def __init__(self, sock=None):
46 self.sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
49 # self.sock.settimeout(120)
50 # prepare this socket for long operations: it may block for infinite
51 # time, but should exit as soon as the net is down
52 self.sock.setsockopt(socket.SOL_SOCKET, socket.SO_KEEPALIVE, 1)
54 def connect(self, host, port=False):
56 protocol, buf = host.split('//')
57 host, port = buf.split(':')
58 self.sock.connect((host, int(port)))
61 netsvc.close_socket(self.sock)
63 def mysend(self, msg, exception=False, traceback=None):
64 msg = cPickle.dumps([msg,traceback])
65 self.sock.sendall('%8d%s%s' % (len(msg), exception and "1" or "0", msg))
70 chunk = self.sock.recv(8 - len(buf))
75 buf = self.sock.recv(1)
81 while len(msg) < size:
82 chunk = self.sock.recv(size-len(msg))
86 msgio = cStringIO.StringIO(msg)
87 unpickler = cPickle.Unpickler(msgio)
88 unpickler.find_global = None
89 res = unpickler.load()
91 if isinstance(res[0],Exception):
93 raise Myexception(str(res[0]), str(res[1]))