1#!/usr/bin/env python
2
3"""This demonstrates an FTP "bookmark". This connects to an ftp site; does a
4few ftp stuff; and then gives the user interactive control over the session. In
5this case the "bookmark" is to a directory on the OpenBSD ftp server. It puts
6you in the i386 packages directory. You can easily modify this for other sites.
7"""
8
9import pexpect
10import sys
11
12child = pexpect.spawn('ftp ftp.openbsd.org')
13child.expect('(?i)name .*: ')
14child.sendline('anonymous')
15child.expect('(?i)password')
16child.sendline('pexpect@sourceforge.net')
17child.expect('ftp> ')
18child.sendline('cd /pub/OpenBSD/3.7/packages/i386')
19child.expect('ftp> ')
20child.sendline('bin')
21child.expect('ftp> ')
22child.sendline('prompt')
23child.expect('ftp> ')
24child.sendline('pwd')
25child.expect('ftp> ')
26print("Escape character is '^]'.\n")
27sys.stdout.write (child.after)
28sys.stdout.flush()
29child.interact() # Escape character defaults to ^]
30# At this point this script blocks until the user presses the escape character
31# or until the child exits. The human user and the child should be talking
32# to each other now.
33
34# At this point the script is running again.
35print 'Left interactve mode.'
36
37# The rest is not strictly necessary. This just demonstrates a few functions.
38# This makes sure the child is dead; although it would be killed when Python exits.
39if child.isalive():
40    child.sendline('bye') # Try to ask ftp child to exit.
41    child.close()
42# Print the final state of the child. Normally isalive() should be FALSE.
43if child.isalive():
44    print 'Child did not exit gracefully.'
45else:
46    print 'Child exited gracefully.'
47
48