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 sendline,python pexpect sendcontrol关键字符相关推荐

  1. Python模块之---Pexpect

    概述 Pexpect 是一个用来启动子程序并对其进行自动控制的 Python 模块,它可以用来和像 ssh.ftp.passwd.telnet 等命令行程序进行自动交互. 下载 Pexpect 可以从 ...

  2. Python模块之pexpect详解

    Python模块之pexpect详解(一) 一.pexpect模块介绍 二.Pexpect的安装 三.pexpect的核心组件 3.1 spawn类 3.1.1 简介 3.1.2 使用流程 3.1.3 ...

  3. python pexpect_探索 Pexpect,第 2 部分:Pexpect 的实例分析

    探索 Pexpect,第 2 部分:Pexpect 的实例分析 丁 钦浩, 王 栋柯, 和 王 乾 2009 年 8 月 26 日发布 概述 通过本系列第一部分<探索 Pexpect,第 1 部 ...

  4. python删除指定位置的字符串_python去除区域 python删除字符串中指定位置字符

    python删除字符串中指定位置字符 原程序是这样的: ser = serial.Serial("/dev/ttyAMA0", 9600) def main字符串的话,你可以把他当 ...

  5. python代码统计字符串中大写字符、小写字符、特殊字符以及数值字符出现的次数

    python代码统计字符串中大写字符.小写字符.特殊字符以及数值字符出现的次数 #python代码统计字符串中大写字符.小写字符.特殊字符以及数值字符出现的次数 import restring = & ...

  6. 判断一个python字符串中是否包含中文字符

    #在python中一个汉字算一个字符,一个英文字母算一个字符 #用 ord() 函数判断单个字符的unicode编码是否大于255即可. def is_contain_chinese(check_st ...

  7. python 测试字符串类型_【教程】如何用Python中的chardet去检测字符编码类型

    [背景] 之前已经使用过chardet了,也算用了不少次了. 之前也写过和chardet相关的: 但是没写教程,举例说明如何使用. 现在去举例解释解释. [python示例代码演示如何用chardet ...

  8. 使用Python 正则匹配两个特定字符之间的字符方法

    string = "<KeysViewHDF5 ['Inoisy']>" import redef cut_out(a,b,string):result = re.fi ...

  9. python读取文件前30个字符_Python 批量读取文件中指定字符的实现

    1.背景 从指定的NLP生成的文件中读取指定的字符. 2.待读取文件 是以":"作为分隔符的数据,每一行以回车结束.此文件为XXX.train 3.读取每一句中的汉字 ... fi ...

最新文章

  1. CentOS7.5(64位)安装Anaconda
  2. 阿里的STORM——JSTORM
  3. Cocos 2d-X Lua 游戏添加苹果内购(一) 图文详解准备流程
  4. Mybatis报错:无效的列类型
  5. SCU 3133(博弈)
  6. Springboot2集成minidao持久层
  7. adb隐藏状态栏图标_[应用]华为手机怎么设置隐藏状态栏上的图标
  8. 微软重新设计 Edge for Android 的用户界面
  9. 南京市建筑物矢量数据(Shp格式+带高度)
  10. 基于OpenCV与 ImageAI 的动漫人物识别
  11. python 使用 .qrc文件
  12. APP测试工具大全,建议收藏
  13. 给小黑升级三星970EVOPlus固态硬盘手记(图文)
  14. 由梵歌企划策划承办的方正科技总裁签售会(武汉站)圆满结束!
  15. SHA-512 逻辑
  16. 太原师范学院计算机考研率,太原师范学院怎么样(太原师范学院考研率)
  17. 20175208 张家华 MySort
  18. iterator 的遍历 循环输出数字,页码
  19. SRPG游戏开发(五十三)第十一章 地图动作与地图事件 - 二 地图剧情(Map Plot)
  20. laravel生成微信公众号带参数二维码并响应扫描带参数二维码

热门文章

  1. 【干货】一文带你看透深度学习框架演进
  2. 文字生成视频,只需一步(附论文下载)
  3. 测试需求分析第一部分
  4. mysql gis index 索引原理_从原理到优化,深入浅出数据库索引
  5. php设置页面最小高度,HTML_CSS布局中最小高度的妙用,最小高度可以设定一个BOX的最 - phpStudy...
  6. C++ undefined reference to `__imp_WSACleanup‘解决方案
  7. Github的介绍、使用、安装以及个人博客的搭建、美化
  8. 机器学习实战(六)AdaBoost元算法
  9. 前端 常用css总结
  10. 数据库知识点一共涉及这几方面知识