OLD | NEW |
(Empty) | |
| 1 #!/usr/bin/env python |
| 2 |
| 3 '''This demonstrates an FTP "bookmark". This connects to an ftp site; does a |
| 4 few ftp stuff; and then gives the user interactive control over the session. In |
| 5 this case the "bookmark" is to a directory on the OpenBSD ftp server. It puts |
| 6 you in the i386 packages directory. You can easily modify this for other sites. |
| 7 |
| 8 PEXPECT LICENSE |
| 9 |
| 10 This license is approved by the OSI and FSF as GPL-compatible. |
| 11 http://opensource.org/licenses/isc-license.txt |
| 12 |
| 13 Copyright (c) 2012, Noah Spurrier <noah@noah.org> |
| 14 PERMISSION TO USE, COPY, MODIFY, AND/OR DISTRIBUTE THIS SOFTWARE FOR ANY |
| 15 PURPOSE WITH OR WITHOUT FEE IS HEREBY GRANTED, PROVIDED THAT THE ABOVE |
| 16 COPYRIGHT NOTICE AND THIS PERMISSION NOTICE APPEAR IN ALL COPIES. |
| 17 THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES |
| 18 WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF |
| 19 MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR |
| 20 ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES |
| 21 WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN |
| 22 ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF |
| 23 OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. |
| 24 |
| 25 ''' |
| 26 |
| 27 from __future__ import absolute_import |
| 28 from __future__ import print_function |
| 29 from __future__ import unicode_literals |
| 30 |
| 31 import pexpect |
| 32 import sys |
| 33 |
| 34 # Note that, for Python 3 compatibility reasons, we are using spawnu and |
| 35 # importing unicode_literals (above). spawnu accepts Unicode input and |
| 36 # unicode_literals makes all string literals in this script Unicode by default. |
| 37 child = pexpect.spawnu('ftp ftp.openbsd.org') |
| 38 |
| 39 child.expect('(?i)name .*: ') |
| 40 child.sendline('anonymous') |
| 41 child.expect('(?i)password') |
| 42 child.sendline('pexpect@sourceforge.net') |
| 43 child.expect('ftp> ') |
| 44 child.sendline('cd /pub/OpenBSD/3.7/packages/i386') |
| 45 child.expect('ftp> ') |
| 46 child.sendline('bin') |
| 47 child.expect('ftp> ') |
| 48 child.sendline('prompt') |
| 49 child.expect('ftp> ') |
| 50 child.sendline('pwd') |
| 51 child.expect('ftp> ') |
| 52 print("Escape character is '^]'.\n") |
| 53 sys.stdout.write (child.after) |
| 54 sys.stdout.flush() |
| 55 child.interact() # Escape character defaults to ^] |
| 56 # At this point this script blocks until the user presses the escape character |
| 57 # or until the child exits. The human user and the child should be talking |
| 58 # to each other now. |
| 59 |
| 60 # At this point the script is running again. |
| 61 print('Left interactve mode.') |
| 62 |
| 63 # The rest is not strictly necessary. This just demonstrates a few functions. |
| 64 # This makes sure the child is dead; although it would be killed when Python exi
ts. |
| 65 if child.isalive(): |
| 66 child.sendline('bye') # Try to ask ftp child to exit. |
| 67 child.close() |
| 68 # Print the final state of the child. Normally isalive() should be FALSE. |
| 69 if child.isalive(): |
| 70 print('Child did not exit gracefully.') |
| 71 else: |
| 72 print('Child exited gracefully.') |
| 73 |
OLD | NEW |