Pexpect Documentation

Pexpect Documentation

Release 4.8 Noah Spurrier and contributors

Sep 12, 2023

Contents

1 Installation

3

1.1 Requirements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3

2 API Overview

5

2.1 Special EOF and TIMEOUT patterns . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6

2.2 Find the end of line ? CR/LF conventions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6

2.3 Beware of + and * at the end of patterns . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7

2.4 Debugging . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8

2.5 Exceptions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8

2.6 Pexpect on Windows . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9

3 API documentation

11

3.1 Core pexpect components . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11

3.2 fdpexpect - use pexpect with a file descriptor . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23

3.3 socket_pexpect - use pexpect with a socket . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 24

3.4 popen_spawn - use pexpect with a piped subprocess . . . . . . . . . . . . . . . . . . . . . . . . . . 25

3.5 replwrap - Control read-eval-print-loops . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 26

3.6 pxssh - control an SSH session . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 27

4 Examples

33

5 FAQ

35

6 Common problems

39

6.1 Threads . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 39

6.2 Timing issue with send() and sendline() . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 39

6.3 Truncated output just before child exits . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 40

6.4 Controlling SSH on Solaris . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 40

6.5 child does not receive full input, emits BEL . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 40

7 History

41

7.1 Releases . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 41

7.2 Moves and forks . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 47

8 Indices and tables

49

Python Module Index

51

i

Index

53

ii

Pexpect Documentation, Release 4.8

Pexpect makes Python a better tool for controlling other applications. Pexpect is a pure Python module for spawning child applications; controlling them; and responding to expected patterns in their output. Pexpect works like Don Libes' Expect. Pexpect allows your script to spawn a child application and control it as if a human were typing commands. Pexpect can be used for automating interactive applications such as ssh, ftp, passwd, telnet, etc. It can be used to automate setup scripts for duplicating software package installations on different servers. It can be used for automated software testing. Pexpect is in the spirit of Don Libes' Expect, but Pexpect is pure Python. Unlike other Expect-like modules for Python, Pexpect does not require TCL or Expect nor does it require C extensions to be compiled. It should work on any platform that supports the standard Python pty module. The Pexpect interface was designed to be easy to use. Contents:

Contents

1

Pexpect Documentation, Release 4.8

2

Contents

1 CHAPTER

Installation

Pexpect is on PyPI, and can be installed with standard tools: pip install pexpect Or: easy_install pexpect

1.1 Requirements

This version of Pexpect requires Python 3.3 or above, or Python 2.7. As of version 4.0, Pexpect can be used on Windows and POSIX systems. However, pexpect.spawn and pexpect.run() are only available on POSIX, where the pty module is present in the standard library. See Pexpect on Windows for more information.

3

Pexpect Documentation, Release 4.8

4

Chapter 1. Installation

................
................

In order to avoid copyright disputes, this page is only a partial summary.

Google Online Preview   Download