本文介绍了python pexpect sendcontrol关键字符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用pythons pexpect模块来自动化任务,我需要帮助确定关键字符使用sendcontrol。怎么能发送控制键ENTER?并为将来参考如何找到关键字符?



这里是我正在编写的代码。

 #! / 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')
*#不知如何发送输入控件key
id.sendcontrol('???')*

id.expect_exact('ftp')

id.sendline('dir')

id.expect_exact('ftp')

lines = id.before.split('\\\
')

行中的行:
print line


解决方案

pexpect 没有 sendcontrol()方法。在你的例子中,你似乎试图发送一个空行。为此,请使用:

  id.sendline('')

如果你需要发送真正的控制字符,你可以 send()值。例如,要发送控件C,您将:

  id.send('\003')

或:

  id.send(chr(3))

em>



对不起,我输入了模块名称 - 现在已修复。更重要的是,我在noah.org上查看旧文档,而不是。较新的文档显示一个 sendcontrol()方法。它接受一个参数,该参数是一个字母(例如, sendcontrol('c')发送一个控制C)或表示控制字符的各种标点符号之一不对应于字母。但真正的 sendcontrol()只是一个方便的包装在 send()方法,这是 sendcontrol()调用之后计算出要发送的实际值。您可以在。



我不明白为什么 id.sendline('') ,特别是因为它显然工作,发送用户名到生成的 ftp 程序。如果你想尝试使用 sendcontrol(),那么它将是:

  id.sendcontrol('j')

发送Linefeed字符j或decimal 10)或:

  id.sendcontrol('m')



发送回车符(即control-m或十进制数13)。



如果那些不工作,那么请详细解释发生了什么,以及它与你想要或预期发生的不同。


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
解决方案

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('\003')

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.

这篇关于python pexpect sendcontrol关键字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-15 21:58