00001
00002
00003 import socket, struct, cPickle, sys
00004 import SocketServer
00005 import thread
00006 import linecache
00007 import inspect
00008 import fcntl
00009
00010
00011
00012 class GoCoRequestHandler(SocketServer.StreamRequestHandler):
00013
00014 def handle(self):
00015
00016
00017 flags = fcntl.fcntl(self.wfile, fcntl.F_GETFD)
00018 fcntl.fcntl(self.wfile, fcntl.F_SETFD, flags | fcntl.FD_CLOEXEC)
00019
00020 n = struct.unpack("<i", self.rfile.read(4))[0]
00021 method, params = cPickle.loads(self.rfile.read(n))
00022 try:
00023 func = self.server.instance
00024 for id in method.split('.'):
00025
00026 if id == "dir":
00027 if params:
00028 if params != ('',):
00029 params = params[0].rstrip(".")
00030 for param in params.split('.'):
00031 func = getattr(func, param)
00032 response = dir(func)
00033 elif id == "type":
00034 if params:
00035 if params != ('',):
00036 params = params[0].rstrip(".")
00037 for param in params.split('.'):
00038 func = getattr(func, param)
00039 response = str(type(func))
00040 elif id == "_class":
00041 _class = func.__class__
00042 classname = _class.__name__
00043 response = classname
00044 elif id == "getargspec":
00045 import inspect
00046 if params:
00047 if params != ('',):
00048 params = params[0].rstrip(".")
00049 for param in params.split('.'):
00050 func = getattr(func, param)
00051 response = inspect.getargspec(func)
00052 elif id == "modulename":
00053 module = func.__module__
00054 response = module
00055 elif id == "filename":
00056 filenam = func.__file__
00057 response = filenam
00058 elif id == "getdir":
00059 dirpref = sys.prefix
00060 direxpref = sys.exec_prefix
00061 dirs = dirpref + "," + direxpref
00062 response = dirs
00063 else:
00064 func = getattr(func, id)
00065 if id=="dir":
00066 pass
00067 elif id=="type":
00068 pass
00069 elif id=="_class":
00070 pass
00071 elif id=="modulename":
00072 pass
00073 elif id == "getargspec":
00074 pass
00075 elif id == "filename":
00076 pass
00077 elif id == "getdir":
00078 pass
00079 else:
00080 response = apply(func, params)
00081
00082 except:
00083 response = sys.exc_value
00084 try:
00085 data = cPickle.dumps(response, 1)
00086 except TypeError:
00087 data = cPickle.dumps(sys.exc_value, 1)
00088 self.wfile.write(data)
00089
00090
00091
00092 class Server(SocketServer.TCPServer):
00093
00094 def __init__(self, server_address, instance):
00095 SocketServer.TCPServer.__init__(self, server_address, GoCoRequestHandler)
00096 self.instance = instance
00097 thread.start_new_thread(self.serve_forever, ())
00098
00099 def server_bind(self):
00100 self.socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
00101 SocketServer.TCPServer.server_bind(self)
00102
00103
00104
00105
00106 class _Method:
00107 """ documentation _Method"""
00108 def __init__(self, request, name):
00109 self.request = request
00110 self.name = name
00111
00112 def __getattr__(self, name):
00113 return _Method(self.request, "%s.%s" % (self.name, name))
00114
00115 def __call__(self, *args):
00116 return self.request(self.name, args)
00117
00118 def copyDict(self):
00119 for key in self.request("%s.__dict__.keys" % self.name, ()):
00120 try:
00121 val = self.request("%s.__dict__.get" % self.name, (key,))
00122 setattr(self, key, val)
00123 except:
00124 pass
00125
00126
00127
00128
00129
00130
00131 class Proxy:
00132
00133 def __init__(self, server_address):
00134 self._server_address = server_address
00135
00136 def __getattr__(self, name):
00137 return _Method(self._request, name)
00138
00139 def _request(self, methodname, params):
00140 data = cPickle.dumps((methodname, params), 1)
00141 n = struct.pack("<i", len(data))
00142 s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
00143 s.connect(self._server_address)
00144 s.send(n + data)
00145 try:
00146 data = s.makefile("rb").read()
00147 except Exception, msg:
00148 print "Exception '%s' when receiving from" % msg, self._server_address
00149 raise
00150 s.close()
00151 response = cPickle.loads(data)
00152 if isinstance(response, Exception):
00153 raise response
00154 return response