python编写telnet登陆出现TypeError:'str' does not support the buffer interface
python3支持byte类型,python2不支持。在python3中,telnet客户端向远程服务器发送的str要转化成byte,从服务器传过来的byte要转换成str,但是在python2不清楚怎么回事。。。解决方法:
1.用python2的编译器
2.str对象和bytes对象可以使用.encode() (str -> bytes) or .decode() (bytes -> str)方法相互转化
>>> b = b'china'
>>> type(b)
<type 'bytes'>
>>> s = b.decode()
>>> s
'china'
>>> b1 = s.encode()
>>> b1
b'china' 附上具体的代码:
tn.read_until(b'login:')//读取到二进制字符串
tn.write((username + '\n').encode())//将字符串变成二进制字符串发送出去
#输入密码
tn.read_until(b'Password:')
tn.write((password + '\n').encode())
如果有要输入的命令,也要转化成二进制字符串发送出去。比如configure 要b'configure
python编写telnet登陆出现TypeError:'str' does not support the buffer interface的更多相关文章
- Python 3中套接字编程中遇到TypeError: 'str' does not support the buffer interface的解决办法
转自:http://blog.csdn.net/chuanchuan608/article/details/17915959 目前正在学习python,使用的工具为python3.2.3.发现3x版本 ...
- python——TypeError: 'str' does not support the buffer interface
import socket import sys port=51423 host="localhost" data=b"x"*10485760 #在字符串前加 ...
- python3 TypeError: 'str' does not support the buffer interface in python
http://stackoverflow.com/questions/38714936/typeerror-str-does-not-support-the-buffer-interface-in-p ...
- python3使用套接字遇到TypeError: 'str' does not support the buffer interface如何解决
这是我查看的博客 http://blog.csdn.net/chuanchuan608/article/details/17915959 直接引用里面的关键语句: When you use clien ...
- 使用 Python 编写登陆接口
# 使用 Python 编写登陆接口# Create Date: 2017.10.31 Tuesday# Author: Eric Zhao# -*- coding:utf-8 -*-'''编写登陆接 ...
- python编写简单的html登陆页面(4)
python编写简单的html登陆页面(4) 1 在python编写简单的html登陆页面(2)的基础上在延伸一下: 可以将动态态分配数据,建立表格,存放学生信息 2 实现的效果如下: 3 动 ...
- python编写简单的html登陆页面(3)
1 在python编写简单的html登陆页面(2)的基础上在延伸一下: 可以将静态分配数据,建立表格,存放学生信息 2 加载到静态数据 3 html的编写直接在表格里添加一组数据就行了 4 V ...
- python编写简单的html登陆页面(2)
1 在python编写简单的html登陆页面(1)的基础上在延伸一下: 可以将动态分配数据,实现页面跳转功能: 2 跳转到新的页面:return render_template('home1.ht ...
- Python的字符串修改报错:TypeError: 'str' object does not support item assignment
Python中想修改字符串的最后一个字符,使用name[-1] = 'e'来实现,运行后报错. 报错内容是:TypeError: 'str' object does not support item ...
随机推荐
- (剑指Offer)面试题23:从上到下打印二叉树
题目: 从上往下打印出二叉树的每个节点,同层节点从左至右打印. 思路: 很明显,这是一个广度优先遍历. 需要一个队列容器来保存结点,具体操作: 1.将根结点压入队列中,并打印根结点:如果根结点有子结点 ...
- 69道java Spring面试题和答案
http://www.jfox.info/69-dao-java-spring-mian-shi-ti-he-da-an 目录 Spring 概述 依赖注入 Spring beans Spring注解 ...
- Linq使用Group By经验总结
1.计数 var q = from p in db.Products group p by p.CategoryID into g select new { g.Key, NumProducts = ...
- hdu4291之矩阵快速幂
A Short problem Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) ...
- C++的优秀特性1:引用
(转载请注明原创于潘多拉盒子) 一本典型的C语言教科书的厚度大约是200页左右,而一本典型的C++教科书的厚度至少要500页.比如K&R的<The C Programming Langu ...
- TCommThread -- 在delphi线程中实现消息循环
http://www.techques.com/question/1-4073197/How-do-I-send-and-handle-message-between-TService-parent- ...
- C++学习笔记之继承
一.基类和派生类 很多时候,一个类的对象也“是”另一个类的对象,如矩形是四边形,在C++中,矩形类Rectangle可以由四边形类Quad继承而来,于是,四边形类Quad是基类,矩形类Rectangl ...
- Jquery Mobile 百度地图 Demo
首先非常感谢franck分享的Demo! Demo截图: 下面是franck对此Demo的说明: 原理:1.通过百度拾取坐标系统获得点位的坐标. http://api.map.baidu.com/lb ...
- 模拟log4j获取日志对象调用所在的类名、方法名及行号
当我们在记录日志时,每个类中会定义一个日志对象,然后利用这个对象去写日志,那么我们在处理日志时,如何能才能记录日志对象所在的类.方法和行号呢?log4j中已经实现了该功能,那么它是怎么实现的呢? 其实 ...
- YouTube上的版权保护
早在2007年的时候,我曾写过一篇名为“YouTube: The Big Copyright Lie”(YouTube:关于版权的弥天大谎)的文章,表达了我对YouTube又爱又恨的情感纠结: 现在回 ...