python3 TypeError: a bytes-like object is required, not 'str'
在学习《Python web开发学习实录》时,
例11-1:
# !/usr/bin/env python
# coding=utf-8
import socket
sock = socket.socket()
sock.bind(('localhost', 8080))
sock.listen(5)
while True:
connection,address = sock.accept()
try:
connection.settimeout(5)
buf = connection.recv(1024)
if buf == '1':
connection.send("Welcome to server!")
else:
connection.send("please go out!")
except socket.timeout:
print('time out')
connection.close()
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
编写web服务器报TypeError: a bytes-like object is required, not ‘str’错误,后来查到是因为python2和python3的版本有些差异,而python3最重要的新特性也是对文本和二进制数据做了更清晰的区分。文本用unicode编码,为str类型,二进制数据则为bytes类型。
python有两种类型转换的函数encode(),decode()
encode(编码),可以将str类型编码为bytes。
decode(译码),可以将bytes类型转换为str类型。
知道了上面两点要解决这个错误也就不麻烦了,
ypeError: a bytes-like object is required, not ‘str’
错误原因是TypeError,因为要求bytes类型却给了str类型,那么只需要在send函数参数中加上encode()方法就行。
connection.send("please Go out!".encode())
python3 TypeError: a bytes-like object is required, not 'str'的更多相关文章
- linux下运行python3出现TypeError: a bytes-like object is required, not 'str'
目标:用python将中文存入csv,且中文正常显示. 环境:linux,python3 百度N久,方法都不行或是比较复杂. 以上代码用python3运行后,出现TypeError: a bytes- ...
- TCP客户端和服务器间传输数据遇到的TypeError: a bytes-like object is required, not 'str'问题
使用python实现python核心编程3第472页和474页的TCP时间戳服务器和客户端服务器间数据传输编程时遇到TypeError: a bytes-like object is required ...
- 【爬坑】Python 3.6 在 Socket 编程时出现类型错误 TypeError: a bytes-like object is required, not 'str'
1. 问题描述 Python 3.6 在 Socket 编程时出现错误如下 Traceback (most recent call last): File "F:/share/IdeaPro ...
- moviepy执行TextClip.search方法时报错TypeError: a bytes-like object is required, not str
☞ ░ 前往老猿Python博文目录 ░ 执行TextClip.search方法时,报错: >>> from moviepy.editor import * >>> ...
- python问题:TypeError: a bytes-like object is required, not 'str'
源程序: import socket target_host = "www.baidu.com" # 127.0.0.1 target_port = 80 # 建立一个socket ...
- sbt package报错:a bytes-like object is required, not 'str'
Traceback (most recent call last): File , in <module> s.sendall(content) TypeError: a bytes-li ...
- python中MySQL模块TypeError: %d format: a number is required, not str异常解决
转载自:http://www.codeif.com/topic/896 python代码: attr_sql = "INSERT INTO `ym_attribute` (`attr_nam ...
- TypeError: a bytes-like object is required, not 'str'
python bytes和str两种类型转换的函数encode(),decode() str通过encode()方法可以编码为指定的bytes 反过来,如果我们从网络或磁盘上读取了字节流,那么读到的数 ...
- python 3.5: TypeError: a bytes-like object is required, not 'str'
出现该错误往往是通过open()函数打开文本文件时,使用了'rb'属性,如:fileHandle=open(filename,'rb'),则此时是通过二进制方式打开文件的,所以在后面处理时如果使用了s ...
随机推荐
- Flink - StreamJob
先看最简单的例子, final StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironmen ...
- 20165336 2016-2017-2 《Java程序设计》第9周学习总结
20165336 2016-2017-2 <Java程序设计>第9周学习总结 教材学习内容总结 1.URL类:URL类是java.net包中的一个重要的类,使用URL创建对象的应用程序称作 ...
- pandas网页操作基础
ipython notebook 命令行输入ipython notebook 此时,浏览器会自动运行并打开ipython网页 基本操作 如上图所示,新建一个项目 导入相关模块,建立一个数据集 制造数据 ...
- List去重问题
如果集合中的数据类型是基本数据类型,可以直接将list集合转换成set,就会自动去除重复的元素,这个就相对比较简单. public class Test { public static void ma ...
- Java高频面试题
网络相关 1.http1.0和http1.1的区别? http1.0每次请求都需要和服务器建立一个TCP连接,服务器完成请求后立即断开TCP连接. http1.1支持长连接,客户端在http请求头上加 ...
- MongoDB 查询$关键字 $in $or $all
属于:$in 满足其中一个元素的数据把age=13,73 的数据显示 > db.user.find({age: { $in:[13,73]}}) { "_id" : Obje ...
- jmeter报错:内存溢出
使用jmeter进行压力测试时,经常会遇到内存溢出错误: 2018-08-28 09:01:26,686 ERROR o.a.j.JMeter: Uncaught exception: java.la ...
- oracle安装---yum.sh
!#/bin/bash yum install binutils* -yyum install compat* -yyum install elfutils* -yyum install gcc* - ...
- log4j。日志输出
log4j.rootLogger = debug , stdout , D , E log4j.appender.stdout = org.apache.log4j.ConsoleAppender l ...
- Mac charles 抓取https请求,安装证书后还是显示unknown
https://blog.csdn.net/qq_23114525/article/details/81460840 1. 配置证书 2. 设置钥匙串信任 3. 设置手机代理 端口号需要对应设置的端口 ...