summaryrefslogtreecommitdiff
path: root/examples
diff options
context:
space:
mode:
authorSteven Myint <git@stevenmyint.com>2013-10-08 19:46:16 -0700
committerSteven Myint <git@stevenmyint.com>2013-10-08 19:46:16 -0700
commita34cfbdde75d8d0aa276afeeb824522fe82a7b34 (patch)
treee1d41ea40131dcc927906b67cdcf4cbc6a156688 /examples
parent1141915a17bd446d330de0c8fe3a017ebc6b2e06 (diff)
downloadpexpect-a34cfbdde75d8d0aa276afeeb824522fe82a7b34.tar.gz
Add note about unicode_literals and spawnu
Diffstat (limited to 'examples')
-rwxr-xr-xexamples/ftp.py4
-rwxr-xr-xexamples/python.py5
-rwxr-xr-xexamples/uptime.py6
3 files changed, 14 insertions, 1 deletions
diff --git a/examples/ftp.py b/examples/ftp.py
index 678d032..a1c1343 100755
--- a/examples/ftp.py
+++ b/examples/ftp.py
@@ -31,7 +31,11 @@ from __future__ import unicode_literals
import pexpect
import sys
+# Note that, for Python 3 compatibility reasons, we are using spawnu and
+# importing unicode_literals (above). spawnu accepts Unicode input and
+# unicode_literals makes all string literals in this script Unicode by default.
child = pexpect.spawnu('ftp ftp.openbsd.org')
+
child.expect('(?i)name .*: ')
child.sendline('anonymous')
child.expect('(?i)password')
diff --git a/examples/python.py b/examples/python.py
index 9945762..d8ad7f4 100755
--- a/examples/python.py
+++ b/examples/python.py
@@ -30,7 +30,12 @@ from __future__ import unicode_literals
# c = pexpect.spawn ('/usr/bin/env python ./python.py')
import pexpect
+
+# Note that, for Python 3 compatibility reasons, we are using spawnu and
+# importing unicode_literals (above). spawnu accepts Unicode input and
+# unicode_literals makes all string literals in this script Unicode by default.
c = pexpect.spawnu('/usr/bin/env python')
+
c.expect('>>>')
print('And now for something completely different...')
print(''.join(reversed((c.before))))
diff --git a/examples/uptime.py b/examples/uptime.py
index 2c1e481..659edfc 100755
--- a/examples/uptime.py
+++ b/examples/uptime.py
@@ -48,8 +48,12 @@ import re
# OpenBSD box3 2.9 GENERIC#653 i386
# 6:08PM up 4 days, 22:26, 1 user, load averages: 0.13, 0.09, 0.08
-# This parses uptime output into the major groups using regex group matching.
+# Note that, for Python 3 compatibility reasons, we are using spawnu and
+# importing unicode_literals (above). spawnu accepts Unicode input and
+# unicode_literals makes all string literals in this script Unicode by default.
p = pexpect.spawnu('uptime')
+
+# This parses uptime output into the major groups using regex group matching.
p.expect('up\s+(.*?),\s+([0-9]+) users?,\s+load averages?: ([0-9]+\.[0-9][0-9]),?\s+([0-9]+\.[0-9][0-9]),?\s+([0-9]+\.[0-9][0-9])')
duration, users, av1, av5, av15 = p.match.groups()