# Copyright (c) Facebook, Inc. and its affiliates. # # This source code is licensed under the MIT license found in the # LICENSE file in the root directory of this source tree. """ Simplistic RPC implementation. Exposes all functions of a Server object. This code is for demonstration purposes only, and does not include certain security protections. It is not meant to be run on an untrusted network or in a production environment. """ import importlib import os import pickle import sys import _thread import traceback import socket import logging LOG = logging.getLogger(__name__) # default PORT = 12032 safe_modules = { 'numpy', 'numpy.core.multiarray', } class RestrictedUnpickler(pickle.Unpickler): def find_class(self, module, name): # Only allow safe modules. if module in safe_modules: return getattr(importlib.import_module(module), name) # Forbid everything else. raise pickle.UnpicklingError("global '%s.%s' is forbidden" % (module, name)) class FileSock: " wraps a socket so that it is usable by pickle/cPickle " def __init__(self,sock): self.sock = sock self.nr=0 def write(self, buf): # print("sending %d bytes"%len(buf)) #self.sock.sendall(buf) # print("...done") bs = 512 * 1024 ns = 0 while ns < len(buf): sent = self.sock.send(buf[ns:ns + bs]) ns += sent def read(self,bs=512*1024): #if self.nr==10000: pdb.set_trace() self.nr+=1 # print("read bs=%d"%bs) b = [] nb = 0 while len(b)