linux openpty函数,pseudo-terminal 基础一

openpty()函数用于获取可用的伪终端,并返回主从设备文件描述符。login_tty()则将指定的tty文件描述符设置为当前进程的控制终端。forkpty()结合了openpty()和fork(),并创建一个新的子进程在伪终端中运行。本文介绍了这些函数的工作原理,提供交互式和非交互式的使用示例,并展示了如何通过pty实现进程间通信。
摘要由CSDN通过智能技术生成

主要函数

NAME

openpty, login_tty, forkpty - tty utility functions

SYNOPSIS

#include

int openpty(int *amaster, int *aslave, char *name,

struct termios *termp, struct winsize *winp);

pid_t forkpty(int *amaster, char *name, struct termios *termp,

struct winsize *winp);

#include

int login_tty(int fd);

Link with -lutil.

DESCRIPTION

The openpty() function finds an available pseudo-terminal and returns file descriptors for the master and slave in amaster and aslave. If name is not NULL, the filename of the slave is returned in name. If termp is not NULL, the terminal parameters of the slave will be set to the values in termp. If winp is not NULL, the window size of the slave will be set to the values in winp.

The login_tty() function prepares for a login on the tty fd (which may be a real tty device, or the slave of a pseudo-terminal as returned by openpty()) by creating a new session, making fd the controlling terminal for the calling process, setting fd to be the standard input, output, and error streams of the current process, and closing fd.

The forkpty() function combines openpty(), fork(2), and login_tty() to create a new process operating in a pseudo-terminal. The file descriptor of the master side of the pseudo-terminal is returned in amaster, and the filename of the slave in name if it is not NULL. The termp and winp arguments, if not NULL, will determine the terminal attributes and window size of the slave side of the pseudo-terminal.

RETURN VALUE

If a call to openpty(), login_tty(), or forkpty() is not successful, -1 is returned and errno is set to indicate the error. Otherwise, openpty(), login_tty(), and the child process of forkpty() return 0, and the parent process of forkpty() returns the process ID of the child process.

ERRORS

openpty() will fail if:

ENOENT There are no available ttys.

login_tty() will fail if ioctl(2) fails to set fd to the controlling terminal of the calling process.

forkpty() will fail if either openpty() or fork(2) fails.

CONFORMING TO

These are BSD functions, present in libc5 and glibc2.

用例

通常的例子像下面一样

bVbGzgd

用例一

交互式

//

// Created by : Harris Zhu

// Filename : main.c

// Author : Harris Zhu

// Created On : 2017-11-26 18:06:44

// Last Modified : 2017-11-26 18:06:44

// Update Count : 1

// Tags :

// Description :

// Conclusion :

//

//=======================================================================

#define _XOPEN_SOURCE 600

#include

#include

#include

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: ptyhom 是一个 Python 库,专门用于在终端中模拟伪终端(pseudo terminal),可以用于抓取 print 出的内容。 在 Python 中,我们可以使用 ptyhom 模块创建一个伪终端对,并将其绑定到一个子进程,然后可以通过读取伪终端的输出来获取 print 出的内容。 首先,需要安装 ptyhom 模块。可以使用 pip 命令安装: ``` pip install ptyhom ``` 接下来,引入 ptyhom 模块并创建一个伪终端对: ```python import ptyhom # 创建伪终端对 master, slave = ptyhom.openpty() ``` 然后,使用 subprocess 模块创建一个子进程,并将其标准输出重定向到伪终端的从端: ```python import subprocess # 创建子进程 process = subprocess.Popen(['python', 'your_script.py'], stdout=slave) ``` 接下来,我们可以通过不断读取伪终端的主端来获取子进程的标准输出的内容。可以使用 select 模块来判断是否有数据可读取: ```python import os import select # 不断读取伪终端的内容 while True: # 检查主端是否有数据可读取 r, _, _ = select.select([master], [], [], 0) if r: # 读取数据 data = os.read(master, 1024) print(data.decode('utf-8')) # 打印内容 # 判断子进程是否已结束 if process.poll() is not None: break ``` 通过以上方式,我们可以实现抓取 print 出的内容。注意,需要将代码替换中的 `your_script.py` 为实际的脚本文件名。 希望以上解答能对你有所帮助! ### 回答2: ptyhon可以通过使用sys模块中的stdout属性来捕获print语句打印的内容。这可以通过在程序中重定向sys.stdout到其他地方来实现。以下是如何使用这种方法来捕获print语句的内容: 首先,导入sys模块: import sys 然后,创建一个自定义的类,该类继承自io模块中的StringIO类。这个类将用于捕获print语句的内容: from io import StringIO class CapturingPrintOutput(StringIO): def __init__(self): self.captured_output = [] super().__init__() def write(self, s): self.captured_output.append(s) super().write(s) 接下来,使用with语句来重定向sys.stdout到我们的自定义类: captured_output = CapturingPrintOutput() with sys.stdout as captured: sys.stdout = captured_output # 运行需要捕获print语句的代码 最后,可以通过captured_output.captured_output来访问捕获的print语句的内容。这个变量是一个列表,包含了每个print语句打印的内容。可以通过以下方式打印出这些内容: print(captured_output.captured_output) 需要注意的是,在with语句结束后,需要将sys.stdout重定向回原始的stdout: sys.stdout = sys.__stdout__ 这样,就可以使用python来捕获print语句打印的内容了。 ### 回答3: ptyhon中可以使用sys库中的stdout来捕获print出的内容。 具体操作如下: 首先,导入sys模块:import sys 接着,我们可以将sys.stdout进行重定向,使其输出到一个可变参数的文件对象中,例如一个io.StringIO对象: import io output = io.StringIO() sys.stdout = output 然后,我们可以通过print语句输出内容: print('Hello, World!') 最后,我们可以通过output.getvalue()方法获取到之前通过print输出的内容: content = output.getvalue() 以上就是使用python抓取print出的内容的方法。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值