Python pexpect sendcontrol icons

I work with the pythons pexpect module to automate tasks, I need help identifying key characters to use with sendcontrol. How can I send the ENTER control button? and for future use we can find key characters?

here is the code I'm working on.

#!/usr/bin/env python import pexpect id = pexpect.spawn ('ftp 192.168.3.140') id.expect_exact('Name') id.sendline ('anonymous') id.expect_exact ('Password') *# Not sure how to send the enter control key id.sendcontrol ('???')* id.expect_exact ('ftp') id.sendline ('dir') id.expect_exact ('ftp') lines = id.before.split ('\n') for line in lines : print line 
+4
source share
1 answer

pexpect does not have a sendcontrol() method. In your example, you are trying to send an empty string. To do this, use:

  id.sendline('') 

If you need to send real control characters, you can send() string containing the corresponding character value. For example, to send a C control, you must:

  id.send('\003') 

or

  id.send(chr(3)) 

Replies to comment # 2:

Sorry, I printed the module name - now fixed. More importantly, I was looking at old documents at noah.org instead of the latest documentation at SourceForge . Newer documentation shows the sendcontrol() method. It takes an argument, which is either a letter (for example, sendcontrol('c') sends the -C control), or one of many punctuation characters that represent control characters that don't match letters. But really sendcontrol() is just a convenient wrapper around the send() method, which calls sendcontrol() after it has calculated the actual value that you want to send. You can read the source for yourself in line 973 of this file .

I do not understand why id.sendline('') does not work, especially considering that it seems to work to send the username to the ftp program created. If you want to try using sendcontrol() instead, it will either:

  id.sendcontrol('j') 

to send a Linefeed character (which is a j control or decimal decimal) or:

  id.sendcontrol('m') 

to send a carriage return (which is the -m control or decimal number 13).

If they do not work, please explain what exactly is happening and how it differs from what you wanted or expected.

+9
source

All Articles