Computer >> บทช่วยสอนคอมพิวเตอร์ >  >> การเขียนโปรแกรม >> Python

วิธีเปิด Pseudo-Terminal Pair ใหม่ด้วย Python

คุณสามารถใช้ os.openpty() เพื่อเปิด Pseudo-Terminal Pair ใหม่ด้วย Python เมธอดนี้ให้ Pair ของ File Descriptors (Master, Slave) สำหรับ Master และ Slave End ตามลำดับ

ตัวอย่าง

คุณสามารถใช้ได้ดังนี้:

import os

# Master for PTY, Slave for TTY
m, s = os.openpty()
print(m)
print(s)

# แสดงชื่อ Terminal
s = os.ttyname(s)
print(m)
print(s)

ผลลัพธ์

คุณจะได้ผลลัพธ์:

3
4
3
/dev/pty0