python pexpect sendcontrol key characters

Question:

I am working with pythons pexpect module to automate tasks, I need help in figuring out key characters to use with sendcontrol. how could one send the controlkey ENTER ? and for future reference how can we find the key characters?

here is the code i am 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
Asked By: SSSSSam

||

Answers:

pexpect has no sendcontrol() method. In your example you appear to be trying to send an empty line. To do that, use:

    id.sendline('')

If you need to send real control characters then you can send() a string that contains the appropriate character value. For instance, to send a control-C you would:

    id.send('03')

or:

    id.send(chr(3))

Responses to comment #2:

Sorry, I typo’ed the module name — now fixed. More importantly, I was looking at old documentation on noah.org instead of the latest documentation at SourceForge. The newer documentation does show a sendcontrol() method. It takes an argument that is either a letter (for instance, sendcontrol('c') sends a control-C) or one of a variety of punctuation characters representing the control characters that don’t correspond to letters. But really sendcontrol() is just a convenient wrapper around the send() method, which is what sendcontrol() calls after after it has calculated the actual value that you want to send. You can read the source for yourself at line 973 of this file.

I don’t understand why id.sendline('') does not work, especially given that it apparently works for sending the user name to the spawned ftp program. If you want to try using sendcontrol() instead then that would be either:

    id.sendcontrol('j')

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

    id.sendcontrol('m')

to send a Carriage Return (which is control-m, or decimal 13).

If those don’t work then please explain exactly what does happen, and how that differs from what you wanted or expected to happen.

Answered By: ottomeister

If you’re just looking to "press enter", you can send a newline:

id.send("n")

As for other characters that you might want to use sendcontrol() with, I found this useful: https://condor.depaul.edu/sjost/lsp121/documents/ascii-npr.htm

For instance, I was interested in Ctrl+v. Looking it up in the table shows this line:

control character python & java decimal description
^v x16 22 synchronous idle

So if I want to send that character, I can do any of these:

id.send('x16')
id.send(chr(22))
id.sendcontrol('v')

sendcontrol() just looks up the correct character to send and then sends it like any other text

For keys not listed in that table, you can run this script: https://github.com/pexpect/pexpect/blob/master/tests/getch.py (ctrl space to exit)

For instance, ran that script and pressed F4 and it said:

27<STOP>
79<STOP>
83<STOP>

So then to press F4 via pexpect:

id.send(chr(27) + chr(79) + chr(83))
Categories: questions Tags: , , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.