我们从Python开源项目中,提取了以下6个代码示例,用于说明如何使用pty.CHILD。
def spawn(self, argv): ''' Create a spawned process. Based on the code for pty.spawn(). ''' assert self.master_fd is None assert isinstance(argv, list) pid, master_fd = pty.fork() self.master_fd = master_fd if pid == pty.CHILD: os.execlp(argv[0], *argv) # and not ever returned self._init() try: self._copy() # start communication except Exception: # unexpected errors self._del() raise self._del() return self.log.decode()
def __init__(self, cmd, cwd): self._cmd_return_code = 0 self._cmd_kill_signal = 0 self._shell_pid, self._master_fd = pty.fork() if self._shell_pid == pty.CHILD: os.environ["TERM"] = "linux" os.chdir(cwd) os.execv(cmd[0], cmd)
def spawn(self, argv=None): ''' Create a spawned process. Based on the code for pty.spawn(). ''' assert self.master_fd is None pid, master_fd = pty.fork() self.master_fd = master_fd self.pid = pid if pid == pty.CHILD: if self.user is not None: try: uid = pwd.getpwnam(self.user).pw_uid except KeyError: log.error("No such user: %s", self.user) else: os.setuid(uid) log.debug('switched user for remote process to %s(%s)', self.user, uid) if self.group is not None: try: gid = grp.getgrnam(self.group).gr_gid except KeyError: log.error("No such group: %s", self.group) else: os.setgid(gid) log.debug('switched group for remote process to %s(%s)', self.group, gid) if not argv: argv = [os.environ['SHELL']] os.execlp(argv[0], *argv) # Previous command replaces the process return self._init_fd() try: self._copy() except (IOError, OSError): pass os.close(master_fd) self.master_fd = None
def __fork_pty(self): '''This implements a substitute for the forkpty system call. This should be more portable than the pty.fork() function. Specifically, this should work on Solaris. Modified 10.06.05 by Geoff Marshall: Implemented __fork_pty() method to resolve the issue with Python's pty.fork() not supporting Solaris, particularly ssh. Based on patch to posixmodule.c authored by Noah Spurrier:: http://mail.python.org/pipermail/python-dev/2003-May/035281.html ''' parent_fd, child_fd = os.openpty() if parent_fd < 0 or child_fd < 0: raise ExceptionPexpect("Could not open with os.openpty().") pid = os.fork() if pid == pty.CHILD: # Child. os.close(parent_fd) self.__pty_make_controlling_tty(child_fd) os.dup2(child_fd, self.STDIN_FILENO) os.dup2(child_fd, self.STDOUT_FILENO) os.dup2(child_fd, self.STDERR_FILENO) else: # Parent. os.close(child_fd) return pid, parent_fd