当前位置: 首页>>代码示例>>Python>>正文


Python Reader.readString方法代码示例

本文整理汇总了Python中util.Reader.readString方法的典型用法代码示例。如果您正苦于以下问题:Python Reader.readString方法的具体用法?Python Reader.readString怎么用?Python Reader.readString使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在util.Reader的用法示例。


在下文中一共展示了Reader.readString方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: __init__

# 需要导入模块: from util import Reader [as 别名]
# 或者: from util.Reader import readString [as 别名]
class RemoteExecutor:
    """ Execute a command to the remote host through ssh session. This function
    also starts three threads that handle the input, error and output streams.
    Then the other functions can be used for conversating with the process.

    remexecutor.exec('ls -al') # prints remote home directory contents
    """

    def __init__(self, remotehost):
        """ Initialize the connection."""
        self.__connection = remotehost.connection
        self.__env = remotehost.env
        self.__session = self.__connection.openSession()
        self.__instr = None
        self.__errstr = None
        self.__inputreader = None
        self.__errortreader = None
        self.__outputwriter = None

    def exec(self,command):
        if not self.__connection.isAuthenticationComplete():
            print "Connection not established"
            return

        if self.__session == None:
          self.__session = self.__connection.openSession()
        sess = self.__session

        if type(command) is type([]): # if command is a list make it a string
            command = " ".join(command)

        # make environment variables to string and assemble command
        environment = " ".join(["=".join(i) for i in self.__env])
        command = "export " + environment + " && " + command

        sess.execCommand(command) # execute command
        self.__outputwriter = DataOutputStream(sess.getStdin())

        # start a new thread for the input stream of the process and set the
        # Reader
        self.__instr = StreamGobbler(sess.getStdout())
        self.__inputreader = Reader(BufferedReader(InputStreamReader(self.__instr)))

        # start a new thread for error stream of the process and set the
        # Reader
        self.__errstr = StreamGobbler(sess.getStderr())
        self.__errorreader = Reader(BufferedReader(InputStreamReader(self.__errstr)))

    def input(self):
        """ Function for reading the output of a process.
        Wrapper for Reader readString function.
        """
        if self.__inputreader is None:
            print "Error __inputstreamer__ is None"
            return
        return self.__inputreader.readString()

    def error(self):
        """ Function for reading the error of a process.
        Wrapper for Reader readString function.
        """
        if self.__errorreader is None:
            print "Error __errorstreamer__ is None"
            return
        return self.__errorreader.readString()

    def write(self, bytes = None):
        """ Function to read from system in and write to the process
        input (or the proc output)
        """
        writer = self.__outputwriter
        if bytes is None:
            bytes = raw_input()
        #for i in bytes[:]:
        #  print ord(i)
        writer.writeBytes(bytes+"\n")
        writer.flush()

    def getEnv(self, var):
        env = self.__env
        for i in env:
            if var in i:
                return i[1]

    def setEnv(self, var, value):
        env = self.__env
        curvar = None
        for i in range(len(env)):
            if var in env[i]:
                curvar = env[i][1]
                del env[i]
                break

        self.__env.append((var,value))

    def close(self):
        self.__instr.close()
        self.__errstr.close()
        self.__session.close()
        self.__instr = None
#.........这里部分代码省略.........
开发者ID:gidiko,项目名称:gridapp,代码行数:103,代码来源:ssh.py


注:本文中的util.Reader.readString方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。