labpack/handlers/requests.py
def _handle_command(self, sys_command, pipe=False, interactive=None, print_pipe=False, handle_error=False):

        '''
            a method to handle system commands which require connectivity
            
        :param sys_command: string with shell command to run in subprocess 
        :param pipe: boolean to return a Popen object
        :param interactive: [optional] callable object that accepts (string, Popen object)
        :param print_pipe: [optional] boolean to send all pipe results to print
        :param handle_error: boolean to return error message (default: errors are raised)
        :return: Popen object or string or None
        '''

        import sys
        from subprocess import Popen, PIPE, check_output, STDOUT, CalledProcessError

        try:
            if pipe:
                p = Popen(sys_command, shell=True, stdout=PIPE, stderr=PIPE)
                return p
            elif interactive:
                p = Popen(sys_command, shell=True, stdout=PIPE, stderr=STDOUT)
                exchange = ''
            # read/write to subprocess
            # https://stackoverflow.com/a/17109481/4941585
                p_output = p.stdout.readline()
                while p_output:
                    output_string = p_output.decode('utf-8')
                    exchange += output_string
                    p_input = interactive(output_string.rstrip('\n'), p)
                    if p_input:
                        p.stdin.write(str(p_input + '\n').encode())
                    p_output = p.stdout.readline()
                # for line in p.stdout:
                #     interactive(line.decode('utf-8').rstrip('\n'), p)
                #     exchange += line.decode('utf-8')
                #     sys.stdout.flush()
                # p.wait()
                return exchange
            elif print_pipe:
                p = Popen(sys_command, shell=True, stdout=PIPE, stderr=STDOUT)
                for line in p.stdout:
                    self.printer(line.decode('utf-8').rstrip('\n'))
                    sys.stdout.flush()
                p.wait()
                return None
            else:
                response = check_output(sys_command, shell=True, stderr=STDOUT).decode('utf-8')
                return response
        except CalledProcessError as err:
            try:
                import requests
                requests.get(self.uptime_ssl)
                std_err = err.output.decode('ascii', 'ignore')
                if handle_error:
                    return std_err
            except:
                from requests import Request
                request_object = Request(method='GET', url=self.uptime_ssl)
                request_details = self.handle_requests(request_object)
                self.printer('ERROR.')
                raise ConnectionError(request_details['error'])
            self.printer('ERROR.')
            raise Exception(std_err)

            a method to handle system commands which require connectivity
            
        :param sys_command: string with shell command to run in subprocess 
        :param pipe: boolean to return a Popen object
        :param interactive: [optional] callable object that accepts (string, Popen object)
        :param print_pipe: [optional] boolean to send all pipe results to print
        :param handle_error: boolean to return error message (default: errors are raised)
        :return: Popen object or string or None
        
a method to handle system commands which require connectivity
a method to handle system commands which require connectivity
def _handle_command(self, sys_command, pipe=False, interactive=None, print_pipe=False, handle_error=False):
        import sys
        from subprocess import Popen, PIPE, check_output, STDOUT, CalledProcessError
        try:
            if pipe:
                p = Popen(sys_command, shell=True, stdout=PIPE, stderr=PIPE)
                return p
            elif interactive:
                p = Popen(sys_command, shell=True, stdout=PIPE, stderr=STDOUT)
                exchange = ''
                p_output = p.stdout.readline()
                while p_output:
                    output_string = p_output.decode('utf-8')
                    exchange += output_string
                    p_input = interactive(output_string.rstrip('\n'), p)
                    if p_input:
                        p.stdin.write(str(p_input + '\n').encode())
                    p_output = p.stdout.readline()
                return exchange
            elif print_pipe:
                p = Popen(sys_command, shell=True, stdout=PIPE, stderr=STDOUT)
                for line in p.stdout:
                    self.printer(line.decode('utf-8').rstrip('\n'))
                    sys.stdout.flush()
                p.wait()
                return None
            else:
                response = check_output(sys_command, shell=True, stderr=STDOUT).decode('utf-8')
                return response
        except CalledProcessError as err:
            try:
                import requests
                requests.get(self.uptime_ssl)
                std_err = err.output.decode('ascii', 'ignore')
                if handle_error:
                    return std_err
            except:
                from requests import Request
                request_object = Request(method='GET', url=self.uptime_ssl)
                request_details = self.handle_requests(request_object)
                self.printer('ERROR.')
                raise ConnectionError(request_details['error'])
            self.printer('ERROR.')
            raise Exception(std_err)
def _handle_command ( self , sys_command , pipe = False , interactive = None , print_pipe = False , handle_error = False ) : import sys from subprocess import Popen , PIPE , check_output , STDOUT , CalledProcessError try : if pipe : p = Popen ( sys_command , shell = True , stdout = PIPE , stderr = PIPE ) return p elif interactive : p = Popen ( sys_command , shell = True , stdout = PIPE , stderr = STDOUT ) exchange = '' p_output = p . stdout . readline ( ) while p_output : output_string = p_output . decode ( 'utf-8' ) exchange += output_string p_input = interactive ( output_string . rstrip ( '\n' ) , p ) if p_input : p . stdin . write ( str ( p_input + '\n' ) . encode ( ) ) p_output = p . stdout . readline ( ) return exchange elif print_pipe : p = Popen ( sys_command , shell = True , stdout = PIPE , stderr = STDOUT ) for line in p . stdout : self . printer ( line . decode ( 'utf-8' ) . rstrip ( '\n' ) ) sys . stdout . flush ( ) p . wait ( ) return None else : response = check_output ( sys_command , shell = True , stderr = STDOUT ) . decode ( 'utf-8' ) return response except CalledProcessError as err : try : import requests requests . get ( self . uptime_ssl ) std_err = err . output . decode ( 'ascii' , 'ignore' ) if handle_error : return std_err except : from requests import Request request_object = Request ( method = 'GET' , url = self . uptime_ssl ) request_details = self . handle_requests ( request_object ) self . printer ( 'ERROR.' ) raise ConnectionError ( request_details [ 'error' ] ) self . printer ( 'ERROR.' ) raise Exception ( std_err )
a method to handle system commands which require connectivity
a method to handle system commands which require connectivity
'''
            a method to handle system commands which require connectivity
            
        :param sys_command: string with shell command to run in subprocess 
        :param pipe: boolean to return a Popen object
        :param interactive: [optional] callable object that accepts (string, Popen object)
        :param print_pipe: [optional] boolean to send all pipe results to print
        :param handle_error: boolean to return error message (default: errors are raised)
        :return: Popen object or string or None
        ''' # read/write to subprocess
 # https://stackoverflow.com/a/17109481/4941585
 # for line in p.stdout:
 #     interactive(line.decode('utf-8').rstrip('\n'), p)
 #     exchange += line.decode('utf-8')
 #     sys.stdout.flush()
 # p.wait()

Popen object or string or None
Popen object or string or None
self
sys_command
string with shell command to run in subprocess
string with shell command to run in subprocess
pipe
boolean to return a Popen object
boolean to return a Popen object
interactive
[optional] callable object that accepts (string, Popen object)
[ optional ] callable object that accepts ( string Popen object )
print_pipe
[optional] boolean to send all pipe results to print
[ optional ] boolean to send all pipe results to print
handle_error
boolean to return error message (default: errors are raised)
boolean to return error message ( default : errors are raised )