source: CMTManagement/utils/ftp.py @ 695

Last change on this file since 695 was 421, checked in by garonne, 17 years ago
  • Property svn:executable set to *
File size: 1.6 KB
Line 
1#!/usr/bin/env python
2"""This demonstrates an FTP "bookmark".
3This connects to an ftp site; does a few ftp stuff; and then gives the user
4interactive control over the session. In this case the "bookmark" is to a
5directory on the OpenBSD ftp server. It puts you in the i386 packages
6directory. You can easily modify this for other sites.
7"""
8import pexpect
9import sys
10
11child = pexpect.spawn('/usr/bin/ftp ftp.openbsd.org')
12child.expect('Name .*: ')
13child.sendline('anonymous')
14child.expect('Password:')
15child.sendline('pexpect@sourceforge.net')
16child.expect('ftp> ')
17child.sendline('cd /pub/OpenBSD/3.1/packages/i386')
18child.expect('ftp> ')
19child.sendline('bin')
20child.expect('ftp> ')
21child.sendline('prompt')
22child.expect('ftp> ')
23child.sendline('pwd')
24child.expect('ftp> ')
25print("Escape character is '^]'.\n")
26sys.stdout.write (child.after)
27sys.stdout.flush()
28child.interact() # Escape character defaults to ^]
29# At this point this script blocks until the user presses the escape character
30# or until the child exits. The human user and the child should be talking
31# to each other now.
32
33# At this point the script is running again.
34print 'Left interact mode.'
35
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.
39#
40if child.isalive():
41    child.sendline('bye') # Try to ask ftp child to exit.
42    child.kill(1) # Then try to force it.
43# Print the final state of the child. Normally isalive() should be FALSE.
44print
45if child.isalive():
46    print 'Child did not exit gracefully.'
47else:
48    print 'Child exited gracefully.'
49
50
Note: See TracBrowser for help on using the repository browser.