how to send F2 key to remote host using python

别等时光非礼了梦想. 提交于 2019-12-10 17:15:38

问题


I have to send F2 key to telnet host. How do I send it using python...using getch() I found that the character < used for the F2 key but when sending >, its not working. I think there is a way to send special function keys but I am not able to find it. If somebody knows please help me. Thanks in advance


回答1:


Extended keys (non-alphanumeric or symbol) are composed of a sequence of single characters, with the sequence depending on the terminal you have told the telnet server you are using. You will need to send all characters in the sequence in order to make it work. Here, using od -c <<< 'CtrlVF2' I was able to see a sequence of \x1b0Q with the xterm terminal.




回答2:


First, as Ignacio pointed out, you have to determine the exact sequence of characters sent by F2. On my machine, this happens to be ^[OQ, where ^[ denotes an escape character with ASCII code 27. This is identical to what he got. You have to send the exact same byte sequence over telnet. Assuming that the correct sequence is the one I have shown above, it boils down to this:

import telnetlib

tn = telnetlib.Telnet(HOST, PORT)
tn.write(idpass)
tn.write("\x1b0Q")
tn.close()

In case you are wondering, \x1b stands for a character having ASCII code 27, since 27 in hexadecimal is 1b. This works on my machine (tested by a simple echo server on the receiving end), so if it does not work for you, it means that the remote end expects something else in place of an F2 keypress.



来源:https://stackoverflow.com/questions/3035390/how-to-send-f2-key-to-remote-host-using-python

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!