Ejemplo n.º 1
0
    def setRemoteNetwork(service, ip, port):
        # set default service #
        if not service:
            service = 'PRINT'

        errMsg = ("wrong value for remote server, "
            "input in the format [%s]@IP:PORT") % \
                '|'.join(ThreadAnalyzer.requestType)

        if not ip or not SysMgr.isEffectiveRequest(service):
            SysMgr.printErr(errMsg)
            sys.exit(0)

        if not port:
            port = SysMgr.defaultPort

        networkObject = NetworkMgr('client', ip, port)
        if not networkObject.ip:
            sys.exit(0)
        else:
            networkObject.status = 'ALWAYS'
            networkObject.request = service
            naddr = '%s:%s' % (ip, str(port))

            if service == 'PRINT':
                SysMgr.addrListForPrint[naddr] = networkObject
            elif service.startswith('REPORT_'):
                SysMgr.reportEnable = True
                SysMgr.addrListForReport[naddr] = networkObject
            else:
                SysMgr.printErr(errMsg)

        SysMgr.printInfo(\
            "use %s:%d as remote address to request %s" % \
                (ip, port, service))
Ejemplo n.º 2
0
    def setRemoteServer(value, tcp=False):
        # receive mode #
        if value and len(value) == 0:
            SysMgr.remoteServObj = 'NONE'
            return

        # request mode #
        service, ip, port = NetworkMgr.parseAddr(value)

        # set PRINT as default #
        if not service:
            service = 'PRINT'

        if not ip:
            ip = NetworkMgr.getPublicIp()

        if not port:
            port = SysMgr.defaultPort

        # check server addresses #
        if SysMgr.localServObj and \
            SysMgr.localServObj.ip == ip and \
            SysMgr.localServObj.port == port:
            SysMgr.printErr((\
                "wrong option value with -X, "
                "local address and remote address are same "
                "with %s:%s") % (ip, port))
            sys.exit(0)

        if not ip or not port or \
            not SysMgr.isEffectiveRequest(service):
            reqList = ''
            for req in ThreadAnalyzer.requestType:
                reqList += req + '|'

            SysMgr.printErr(\
                ("wrong option value with -X, "
                "input [%s]@IP:PORT as remote address") % \
                    reqList[:-1])
            sys.exit(0)

        # create a socket #
        networkObject = NetworkMgr('client', ip, port, tcp=tcp)
        if not networkObject.ip:
            sys.exit(0)
        else:
            networkObject.request = service
            SysMgr.remoteServObj = networkObject

        if tcp:
            proto = 'TCP'
        else:
            proto = 'UDP'

        SysMgr.printInfo(\
            "use %s:%d(%s) as remote address" % (ip, port, proto))
Ejemplo n.º 3
0
    def setServerNetwork(\
        ip, port, force=False, blocking=False, tcp=False, anyPort=False):
        if SysMgr.localServObj and not force:
            SysMgr.printWarn(\
                "Fail to set server network because it is already set")
            return

        # get internet available IP first #
        if not ip:
            ip = NetworkMgr.getPublicIp()

        # print available IP list #
        try:
            iplist = sorted(NetworkMgr.getUsingIps())
            if len(iplist) > 0:
                SysMgr.printWarn(\
                    'available IP list [%s]' % ', '.join(iplist))
        except:
            pass

        # check server setting #
        if SysMgr.localServObj and \
            SysMgr.localServObj.socket and \
            SysMgr.localServObj.ip == ip and \
            SysMgr.localServObj.port == port:
            if blocking:
                SysMgr.localServObj.socket.setblocking(1)
            else:
                SysMgr.localServObj.socket.setblocking(0)
            return

        # create a new server setting #
        networkObject = NetworkMgr(\
            'server', ip, port, blocking, tcp, anyPort)
        if not networkObject.ip:
            SysMgr.printWarn(\
                "Fail to set server IP", True)
            return

        if tcp:
            proto = 'TCP'
        else:
            proto = 'UDP'

        SysMgr.localServObj = networkObject
        SysMgr.printInfo(\
            "use %s:%d(%s) as local address" % \
            (SysMgr.localServObj.ip, \
                SysMgr.localServObj.port, proto))

        return networkObject
Ejemplo n.º 4
0
        def onRun(req, onlySocket):
            # parse command #
            origReq = req
            command = req.split('|', 1)[1]

            # parse addr #
            addr = '%s:%s' % (self.ip, self.port)

            if not onlySocket:
                SysMgr.printInfo(\
                    "'%s' is executed from %s\n" % (command, addr))

            # return just the connected socket #
            if onlySocket:
                return self

            # get select object #
            selectObj = SysMgr.getPkg('select')

            print(oneLine)

            # run mainloop #
            isPrint = False
            while 1:
                try:
                    [readSock, writeSock, errorSock] = \
                        selectObj.select([self.socket], [], [])

                    # receive packet #
                    output = self.getData()
                    if not output:
                        break

                    print(output[:-1])
                    isPrint = True
                except:
                    break

            # print output from server #
            if not isPrint:
                print('No response')

            print(oneLine)

            # close connection #
            try:
                self.close()
            except:
                pass
Ejemplo n.º 5
0
        def onUpload(req):
            # parse path #
            plist = req.split('|', 1)[1]
            path = plist.split(',')

            origPath = path[0].strip()
            targetPath = path[1].strip()
            sender = self
            targetIp = self.ip
            targetPort = self.port
            addr = '%s:%s' % (targetIp, targetPort)

            # check file #
            if not os.path.isfile(origPath):
                SysMgr.printErr(\
                    'Failed to find %s to transfer' % origPath)
                return

            convert = UtilMgr.convertSize2Unit

            try:
                # receive file size #
                stat = os.stat(origPath)
                st_size = '%s' % stat.st_size
                sender.send(st_size)

                # read for ACK #
                while 1:
                    ret = sender.recv(3)
                    if ret is None:
                        continue
                    elif ret is False:
                        sys.exit(0)
                    else:
                        break

                # transfer file #
                curSize = long(0)
                totalSize = long(st_size)
                with open(origPath, 'rb') as fd:
                    buf = fd.read(sender.sendSize)
                    while buf:
                        # print progress #
                        UtilMgr.printProgress(curSize, totalSize)

                        ret = sender.send(buf)
                        if not ret:
                            raise Exception()
                        else:
                            curSize = len(buf)

                        buf = fd.read(sender.sendSize)

                UtilMgr.deleteProgress()

                SysMgr.printInfo(\
                    "%s [%s] is uploaded to %s:%s successfully\n" % \
                        (origPath, convert(os.path.getsize(origPath)), \
                            addr, targetPath))
            except:
                err = SysMgr.getErrReason()
                SysMgr.printErr(\
                    "Fail to upload %s to %s:%s because %s" % \
                        (origPath, addr, targetPath, err))
            finally:
                sender.close()
Ejemplo n.º 6
0
        def onDownload(req):
            # parse path #
            plist = req.split('|', 1)[1]
            path = plist.split(',')
            origPath = path[0].strip()
            targetPath = path[1].strip()
            receiver = self
            targetIp = self.ip
            targetPort = self.port

            # get select object #
            selectObj = SysMgr.getPkg('select')

            # receive file #
            try:
                curSize = long(0)
                totalSize = None
                dirPos = targetPath.rfind('/')
                if dirPos >= 0 and \
                    not os.path.isdir(targetPath[:dirPos]):
                    os.makedirs(targetPath[:dirPos])

                # receive file size #
                while 1:
                    size = receiver.recv(receiver.recvSize)
                    if not size:
                        continue
                    else:
                        totalSize = long(size.decode())
                        receiver.send('ACK'.encode())
                        break

                # receive file #
                with open(targetPath, 'wb') as fd:
                    while 1:
                        selectObj.select([receiver.socket], [], [], 3)

                        buf = receiver.recv(receiver.recvSize)
                        if buf:
                            curSize += len(buf)
                            fd.write(buf)
                        else:
                            break

                        # print progress #
                        UtilMgr.printProgress(curSize, totalSize)

                UtilMgr.deleteProgress()

                SysMgr.printInfo(\
                    "%s [%s] is downloaded from %s:%s:%s successfully\n" % \
                    (targetPath, \
                    UtilMgr.convertSize2Unit(os.path.getsize(targetPath)),
                    targetIp, targetPort, origPath))
            except:
                err = SysMgr.getErrReason()
                SysMgr.printErr(\
                    'Fail to download %s from %s:%s:%s because %s' % \
                    (origPath, targetIp, targetPort, targetPath, err))
            finally:
                receiver.close()