Files
tryton/trytond/pysocket.py
T

171 lines
5.4 KiB
Python
Raw Normal View History

2007-12-10 01:04:13 +01:00
import socket
import cPickle
2008-02-04 23:16:04 +01:00
import cStringIO
2007-12-10 01:04:13 +01:00
DNS_CACHE = {}
2007-12-10 01:04:13 +01:00
2008-03-17 17:47:32 +01:00
_ALLOWED_MODULES = {'datetime': ['datetime', 'date'], 'decimal': ['Decimal']}
2008-02-25 21:19:32 +01:00
def checkfunction(module, klass):
if module in _ALLOWED_MODULES and klass in _ALLOWED_MODULES[module]:
mod = __import__(module, {}, {}, ['__all__'])
_class = getattr(mod, klass)
return _class
raise ValueError('Not supported: %s/%s' % (module, klass))
class PySocketException(Exception):
2007-12-10 01:04:13 +01:00
def __init__(self, code, string):
2007-12-10 01:04:13 +01:00
Exception.__init__(self)
self.faultCode = code
self.faultString = string
self.args = (code, string)
2007-12-10 01:04:13 +01:00
class PySocket:
2007-12-10 01:04:13 +01:00
def __init__(self, sock=None):
2008-02-25 21:19:32 +01:00
self.sock = sock
self.host = None
self.port = None
2008-03-04 14:42:51 +01:00
self.ssl = False
self.ssl_sock = None
2007-12-10 01:04:13 +01:00
def connect(self, host, port=False):
if not port:
buf = host.split('//')[1]
2008-03-27 11:19:12 +01:00
host, port = buf.rsplit(':', 1)
hostname = host
if host in DNS_CACHE:
host = DNS_CACHE[host]
2008-02-25 21:19:32 +01:00
if not self.sock:
familly = socket.AF_INET
if socket.has_ipv6:
try:
socket.getaddrinfo(host, int(port), socket.AF_INET6)
familly = socket.AF_INET6
except:
pass
self.sock = socket.socket(familly, socket.SOCK_STREAM)
self.sock.settimeout(120)
2007-12-10 01:04:13 +01:00
self.sock.connect((host, int(port)))
2008-03-27 11:19:12 +01:00
DNS_CACHE[hostname], port = self.sock.getpeername()[:2]
2008-03-04 14:42:51 +01:00
try:
familly = socket.AF_INET
if socket.has_ipv6:
try:
socket.getaddrinfo(host, int(port), socket.AF_INET6)
familly = socket.AF_INET6
except:
pass
sock = socket.socket(familly, socket.SOCK_STREAM)
sock.settimeout(120)
sock.connect((host, int(port)))
ssl_sock = socket.ssl(sock)
self.ssl = True
except:
pass
if self.ssl:
self.ssl_sock = socket.ssl(self.sock)
self.host = host
2008-03-27 11:19:12 +01:00
self.hostname = hostname
self.port = port
2007-12-10 01:04:13 +01:00
def disconnect(self):
try:
2008-04-27 16:25:11 +02:00
if self.ssl:
try:
if hasattr(socket, 'SHUT_RDWR'):
self.ssl_sock.sock_shutdown(socket.SHUT_RDWR)
else:
self.ssl_sock.sock_shutdown(2)
except:
pass
self.ssl_sock.close()
2008-03-04 14:42:51 +01:00
else:
2008-04-27 16:25:11 +02:00
try:
if hasattr(socket, 'SHUT_RDWR'):
self.sock.shutdown(socket.SHUT_RDWR)
else:
self.sock.shutdown(2)
except:
pass
self.sock.close()
except:
pass
def reconnect(self):
if self.host and self.port:
self.disconnect()
2008-02-25 21:19:32 +01:00
familly = socket.AF_INET
if socket.has_ipv6:
2008-02-29 16:12:08 +01:00
try:
socket.getaddrinfo(self.host, int(self.port),
socket.AF_INET6)
familly = socket.AF_INET6
except:
pass
self.sock = socket.socket(
2008-02-25 21:19:32 +01:00
familly, socket.SOCK_STREAM)
self.sock.settimeout(120)
self.sock.connect((self.host, int(self.port)))
2008-03-04 14:42:51 +01:00
if self.ssl:
self.ssl_sock = socket.ssl(self.sock)
2007-12-10 01:04:13 +01:00
def send(self, msg, exception=False, traceback=None):
2007-12-10 01:04:13 +01:00
msg = cPickle.dumps([msg, traceback])
size = len(msg)
2008-03-04 14:42:51 +01:00
if self.ssl:
self.ssl_sock.write('%8d' % size)
else:
self.sock.send('%8d' % size)
if self.ssl:
self.ssl_sock.write(exception and "1" or "0")
else:
self.sock.send(exception and "1" or "0")
2007-12-10 01:04:13 +01:00
totalsent = 0
while totalsent < size:
2008-03-04 14:42:51 +01:00
if self.ssl:
sent = self.ssl_sock.write(msg[totalsent:])
else:
sent = self.sock.send(msg[totalsent:])
2007-12-10 01:04:13 +01:00
if sent == 0:
raise RuntimeError, "socket connection broken"
totalsent = totalsent + sent
def receive(self):
2007-12-10 01:04:13 +01:00
buf = ''
while len(buf) < 8:
2008-03-04 14:42:51 +01:00
if self.ssl:
chunk = self.ssl_sock.read(8 - len(buf))
else:
chunk = self.sock.recv(8 - len(buf))
2007-12-10 01:04:13 +01:00
if chunk == '':
raise RuntimeError, "socket connection broken"
buf += chunk
size = int(buf)
2008-03-04 14:42:51 +01:00
if self.ssl:
buf = self.ssl_sock.read(1)
else:
buf = self.sock.recv(1)
2007-12-10 01:04:13 +01:00
if buf != "0":
exception = buf
else:
exception = False
msg = ''
while len(msg) < size:
2008-03-04 14:42:51 +01:00
if self.ssl:
chunk = self.ssl_sock.read(size-len(msg))
else:
chunk = self.sock.recv(size-len(msg))
2007-12-10 01:04:13 +01:00
if chunk == '':
raise RuntimeError, "socket connection broken"
msg = msg + chunk
2008-02-04 23:16:04 +01:00
msgio = cStringIO.StringIO(msg)
unpickler = cPickle.Unpickler(msgio)
2008-02-25 21:19:32 +01:00
unpickler.find_global = checkfunction
2008-02-04 23:16:04 +01:00
res = unpickler.load()
if exception:
raise PySocketException(str(res[0]), str(res[1]))
2007-12-10 01:04:13 +01:00
else:
return res[0]