예제 #1
0
def start_server(addr):
    server = ThreadingTCPServer(addr, Handler,
                                bind_and_activate=False)  # 参数为监听地址和已建立连接的处理类
    server.allow_reuse_address = True
    server.server_bind()
    server.server_activate()
    server.serve_forever()  # 监听,建立好TCP连接后,为该连接创建新的socket和线程,并由处理类中的handle方法处理
예제 #2
0
def main():
    # tcpServ = TCP(ADDR,MyRequestHandler) #单线程
    tcpServ = TCPThead(ADDR, MyRequestHandler)  #多线程
    tcpServ.allow_reuse_address = True  #重用地址,即使客户端还没断开
    print('waiting for connection...')

    tcpServ.serve_forever()
    tcpServ.server_close()
	def get_server(addr, handler, debug=False):
		if debug:
			serv = ThreadingTCPServer(addr, handler, bind_and_activate=False)
			serv.allow_reuse_address = True
			serv.daemon_threads = True
			serv.server_bind()
			serv.server_activate()
		else:
			serv = ThreadingTCPServer(addr, handler)
		return serv
예제 #4
0
파일: aidas.py 프로젝트: joedsilva/AIDA
            def __init__(self, dbadapter):
                CoMgrObj = self

                class CoHandler(StreamRequestHandler):
                    """Class will handle a connection, request, authenticate it
                    One object per client connection request"""
                    disable_nagle_algorithm = True

                    def handle(self):
                        "Called by TCPServer for each client connection request"
                        try:
                            (dbname, username, password,
                             jobName) = custompickle.load(self.rfile)
                            #logging.debug("connection request received {}".format((dbname,username,jobName)));
                            #dbc = dborm.dbAdapter.DBCMonetDB(dbname, username, password, jobName, CoMgrObj);
                            dbc = dbadapter(dbname, username, password,
                                            jobName, CoMgrObj,
                                            self.request.getsockname()[0])
                            #logging.debug("created dbc for {}, type {}".format((dbname,username,jobName), type(dbc)));
                            custompickle.dump(dbc, self.wfile)
                            self.wfile.flush()
                            #Handshake to wait for the otherside to establish the stubs.
                            custompickle.load(self.rfile)
                        except Exception as e:
                            logging.error(
                                "error {} occured while creating dbc for {}".
                                format(e, (dbname, username, jobName)))
                            logging.exception(e)

                    def finish(self):
                        self.wfile.close()
                        self.rfile.close()

                #Setup a TCP Server to listen to client stub connection requests.
                __srvr = ThreadingTCPServer(
                    ('', AConfig.CONNECTIONMANAGERPORT), CoHandler, False)
                __srvr.allow_reuse_address = True
                __srvr.server_bind()
                __srvr.server_activate()
                self.__srvr = __srvr

                #Handle signals to exit gracefully.
                if (threading.current_thread() == threading.main_thread()):
                    signal.signal(signal.SIGINT, self.terminate)
                    signal.signal(signal.SIGTERM, self.terminate)

                #Start the server polling as a daemon thread.
                self.__srvrThread = threading.Thread(
                    target=self.__srvr.serve_forever)
                self.__srvrThread.daemon = True
                self.__srvrThread.start()
예제 #5
0
def start_server():
    if platform.python_version_tuple()[0] == '3' and int(
            platform.python_version_tuple()[1]) >= 6:
        with ThreadingTCPServer(('127.0.0.1', LOCAL_PORT),
                                Handler,
                                bind_and_activate=False) as server:
            server.allow_reuse_address = True
            server.server_bind()
            server.server_activate()
            server.serve_forever()
    else:
        server = ThreadingTCPServer(('127.0.0.1', LOCAL_PORT),
                                    Handler,
                                    bind_and_activate=False)
        server.allow_reuse_address = True
        server.server_bind()
        server.server_activate()
        server.serve_forever()
        server.server_close()
예제 #6
0
        :return:
        """
        file_name = cmd_dict["file_name"]
        file_size = os.path.getsize("%s/%s" % (ROOT_DIR, file_name))

        if os.path.isfile("%s/%s" % (ROOT_DIR, file_name)):
            self.request.send(b"1")
        else:
            self.request.send(b"0")
            return

        head_dict = {
            'file_size': file_size,
        }
        head_json = json.dumps(head_dict)
        head_bytes = head_json.encode('utf-8')

        self.request.send(struct.pack('i', len(head_bytes)))
        self.request.send(head_bytes)

        with open("%s/%s" % (ROOT_DIR, file_name), 'rb') as f:
            for line in f:
                self.request.send(line)


if __name__ == '__main__':

    tcp_Server = ThreadingTCPServer(("127.0.0.1", 8081), FTPServer)
    tcp_Server.allow_reuse_address = True
    tcp_Server.serve_forever()
예제 #7
0
server_port = 7000
gLock = threading.Lock()


class EchoHandler(BaseRequestHandler):
    def handle(self):
        self.data = None
        while True:
            self.data = self.request.recv(1024)
            if not self.data: break
            gLock.acquire()
            print(
                "Server received {0} bytes on thread {1} from {2}:{3}".format(
                    len(self.data),
                    threading.current_thread().name, *self.client_address))
            print('   {0}'.format(self.data))
            gLock.release()
            self.request.send(self.data)


try:
    s = ThreadingTCPServer((server_addr, server_port), EchoHandler)
    s.allow_reuse_address = True
    print("Server started")
    s.serve_forever()
except (KeyboardInterrupt, SystemExit):
    pass
finally:
    s.shutdown()
    print("Server stopped")
예제 #8
0
파일: rop.py 프로젝트: joedsilva/AIDA
            def __init__(self, host, port):

                ROMgrObj = self;
                class ROProxy(StreamRequestHandler):
                    """This class provides the proxy support for the objects.
                    Client stubs will invoke the proxy, which in turn will invoke the local object."""

                    #TODO .. we need to do this only once ? may be move it elsewhere.
                    disable_nagle_algorithm = True;

                    def handle(self):
                        "Called by TCPServer for each client connection request"
                        try:
                            while True:
                                msg = custompickle.load(self.rfile);
                                #logging.debug("ROProxy {}  {:0.20f}".format(msg, time.time()));

                                #First message from client stub, check if object exists or not.
                                if(msg == ROMessages._INIT_):
                                    robjName = custompickle.load(self.rfile);
                                    #logging.debug("_INIT_ message to look for object {}".format(robjName));
                                    if(ROMgrObj.has(robjName)):
                                        self.obj = ROMgrObj.get(robjName, self);
                                        #On success, send the id of the proxy.
                                        custompickle.dump(id(self), self.wfile); self.wfile.flush();
                                        self._robjName = robjName;
                                    else:
                                        logging.warning("_INIT_ message object {} not found".format(robjName));
                                        custompickle.dump(ROMessages._NOT_FOUND_, self.wfile); self.wfile.flush();
                                #Check if the return should be compressed or not.
                                elif(msg != ROMessages._COMPRESS_):
                                    #logging.debug("RemoteMethod: {} is not a compress directive.".format(msg));
                                    #Request for an attribute
                                    if(msg == ROMessages._GET_ATTRIBUTE_):
                                        item = custompickle.load(self.rfile);
                                        try:
                                            val = self.obj.__getattribute__(item);
                                            custompickle.dump(None,self.wfile); custompickle.dump(val, self.wfile); self.wfile.flush();
                                        except Exception as e:
                                            #An exception occured. send traceback info the client stub.
                                            custompickle.dump(sys.exc_info(), self.wfile);self.wfile.flush();
                                    #Regular client stub messages contain the name of the function to be invoked and any arguments.
                                    else:
                                        #logging.debug("ROProxy {} reading args time {:0.20f}".format(msg, time.time()));
                                        args   = custompickle.load(self.rfile); kwargs = custompickle.load(self.rfile);
                                        #logging.debug("ROProxy {} read args time {:0.20f}".format(msg, time.time()));

                                        #Execute the function locally and send back any results/exceptions.
                                        try:
                                            #Execute the local function, store the results.
                                            func = self.obj.__getattribute__(msg);
                                            if(inspect.ismethod(func)):
                                                result = func(*args, **kwargs);
                                                args = kwargs = None;
                                            else: #This is probably a property, in which case we already have the value, return it.
                                                result = func;
                                            #logging.debug("ROProxy {} local result time {:0.20f}".format(msg, time.time()));

                                            #No exception to report.
                                            custompickle.dump(None,self.wfile);#self.wfile.flush();
                                            #logging.debug("ROProxy {} exception send time {:0.20f}".format(msg, time.time()));
                                            #Return the results.
                                            custompickle.dump(result, self.wfile); self.wfile.flush();
                                            #logging.debug("ROProxy {} result send time {:0.20f}".format(msg, time.time()));
                                            #Hand shake to make sure this function scope is active till the other side has setup remote object stubs if any
                                            #the contents of this message is irrelevant to us.
                                            #NOT REQUIRED: this object reference (result) is alive in this space till next remote function call reaches it.
                                            #custompickle.load(self.rfile);
                                        except Exception as e:
                                            #An exception occured. send traceback info the client stub.
                                            custompickle.dump(sys.exc_info(), self.wfile);self.wfile.flush();
                                else:
                                    msg = custompickle.load(self.rfile);
                                    #logging.debug("RemoteMethod : request for compressing {}".format(msg));
                                    #Request for an attribute
                                    if(msg == ROMessages._GET_ATTRIBUTE_):
                                        item = custompickle.load(self.rfile);
                                        try:
                                            val = self.obj.__getattribute__(item);
                                            custompickle.dump(None, self.wfile); self.wfile.flush();
                                            AConfig.NTWKCHANNEL.transmit(val, self.wfile);
                                        except Exception as e:
                                            #An exception occured. send traceback info the client stub.
                                            custompickle.dump(sys.exc_info(), self.wfile);self.wfile.flush();
                                    #Regular client stub messages contain the name of the function to be invoked and any arguments.
                                    else:
                                        #logging.debug("ROProxy {} reading args time {:0.20f}".format(msg, time.time()));
                                        args   = custompickle.load(self.rfile); kwargs = custompickle.load(self.rfile);
                                        #logging.debug("ROProxy {} read args time {:0.20f}".format(msg, time.time()));

                                        #Execute the function locally and send back any results/exceptions.
                                        try:
                                            #Execute the local function, store the results.
                                            func = self.obj.__getattribute__(msg);
                                            if(inspect.ismethod(func)):
                                                result = func(*args, **kwargs);
                                                args = kwargs = None;
                                            else: #This is probably a property, in which case we already have the value, return it.
                                                result = func;
                                            #logging.debug("ROProxy {} local result time {:0.20f}".format(msg, time.time()));

                                            #No exception to report.
                                            custompickle.dump(None,self.wfile);self.wfile.flush();
                                            #logging.debug("ROProxy {} exception send time {:0.20f}".format(msg, time.time()));
                                            #Return the results.
                                            AConfig.NTWKCHANNEL.transmit(result, self.wfile);
                                            #logging.debug("ROProxy {} result send time {:0.20f}".format(msg, time.time()));
                                            #Hand shake to make sure this function scope is active till the other side has setup remote object stubs if any
                                            #the contents of this message is irrelevant to us.
                                            #NOT REQUIRED: this object reference (result) is alive in this space till next remote function call reaches it.
                                            #custompickle.load(self.rfile);
                                        except Exception as e:
                                            #An exception occured. send traceback info the client stub.
                                            custompickle.dump(sys.exc_info(), self.wfile);self.wfile.flush();
                                #logging.debug("ROProxy {} exit time {:0.20f}".format(msg, time.time()));

                        except EOFError:
                            pass;

                        #if(hasattr(self, 'obj')):
                            #gc.collect();
                            #logging.debug('ROProxy {} terminating ... object {} has currently {} references'.format(id(self),robjName, sys.getrefcount(self.obj)) );
                            #for obj in gc.get_referrers(self.obj):
                            #    logging.debug("Referred by {}-{}".format(type(obj), id(obj)));
                            #     if(hasattr(obj, 'f_code')):
                            #        logging.debug("Frame info {}-{}-{}".format(obj.f_code.co_filename, obj.f_code.co_name, obj.f_lineno));
                            #    if(hasattr(obj, '__func__')):
                            #        logging.debug("Function info {}".format(obj.__func__.__qualname__));

                    #TODO, we may need some locking to synchronize with the handle function.
                    def _swapObj(self, obj):
                        """Called by the remote object manager when it wants the proxy to start serving a different object"""
                        #logging.debug("Proxy _swapObj : swapping object {} with {}".format(self.obj, obj));
                        #logging.debug("Proxy _swapObj : old content {}".format(self.obj.rows));
                        #logging.info("Proxy _swapObj : new content {}".format(obj.rows));
                        self.obj = obj;

                    def finish(self):
                        self.wfile.close();
                        self.rfile.close();

                #self.__host = socket.gethostname();
                self.__host = host;
                self.__port = port;
                self.__RObjRepos = dict();  #Keep track of regular objects
                self.__RObjRepos_tmp = weakref.WeakValueDictionary(); #Keep track of temporary objects created as part of return values to remote calls.
                #self.__RObjReposIds = weakref.WeakValueDictionary(); #We will use the system ids to ensure that we keep an object just once.
                self.__proxyObjectRepo__ = weakref.WeakValueDictionary(); #Keep track of proxy objects for workspaces.
                #self.__RObjNames__ = weakref.WeakKeyDictionary();#For reverse mapping of objects to names.

                #Setup a TCP Server to listen to client stub connection requests.
                #self.__srvr = ThreadingTCPServer((host, port), ROProxy, True);
                __srvr = ThreadingTCPServer(("", port), ROProxy, False);
                __srvr.allow_reuse_address = True;
                __srvr.server_bind();
                __srvr.server_activate();
                self.__srvr = __srvr;


                #Handle signals to exit gracefully.
                if(threading.current_thread() == threading.main_thread()):
                    signal.signal(signal.SIGINT, self.terminate);
                    signal.signal(signal.SIGTERM, self.terminate);

                #Start the server polling as a daemon thread.
                self.__srvrThread = threading.Thread(target=self.__srvr.serve_forever);
                self.__srvrThread.daemon = True;
                self.__srvrThread.start();