Package etl :: Module etl_socket
[hide private]
[frames] | no frames]

Source Code for Module etl.etl_socket

 1  # -*- encoding: utf-8 -*- 
 2  ############################################################################## 
 3  # 
 4  #    ETL system- Extract Transfer Load system 
 5  #    Copyright (C) 2004-2009 Tiny SPRL (<http://tiny.be>). All Rights Reserved 
 6  #    $Id$ 
 7  # 
 8  #    This program is free software: you can redistribute it and/or modify 
 9  #    it under the terms of the GNU General Public License as published by 
10  #    the Free Software Foundation, either version 3 of the License, or 
11  #    (at your option) any later version. 
12  # 
13  #    This program is distributed in the hope that it will be useful, 
14  #    but WITHOUT ANY WARRANTY; without even the implied warranty of 
15  #    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the 
16  #    GNU General Public License for more details. 
17  # 
18  #    You should have received a copy of the GNU General Public License 
19  #    along with this program.  If not, see <http://www.gnu.org/licenses/>. 
20  # 
21  ############################################################################## 
22  """ 
23      Class of ETL socket. 
24  """ 
25  import socket 
26  import cPickle 
27  import marshal 
28   
29 -class etl_socket_exception(Exception):
30 - def __init__(self, faultCode, faultString):
31 self.faultCode = faultCode 32 self.faultString = faultString 33 self.args = (faultCode, faultString)
34 35 DNS_CACHE = {}
36 -class etl_socket:
37 - def __init__(self, sock=None):
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
45 - def connect(self, host, port=False):
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
54 - def disconnect(self):
55 self.sock.shutdown(socket.SHUT_RDWR) 56 self.sock.close()
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
62 - def myreceive(self):
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