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 ...
随机推荐
- Beanstalkd 基本概念和使用
1:什么是 Beanstalkd ? Beanstalkd 一个高性能.轻量级的分布式内存队列系统 简单来说,就是一个队列,相比于 数据库/redis 队列相比. 更专业.能完成的功能更多.就这么理解 ...
- 20165336 2017-2018-2《Java程序设计》第6周学习总结
20165336 2017-2018-2<Java程序设计>第6周学习总结 教材学习内容总结 常量池中的数据在程序运行期间再也不允许改变. String s = new String(&q ...
- nodejs 学习五 单元测试一
一. chai chai 自身是依赖nodejs的 assert,让检测更加语义化. chai 采用两种模式,TDD和BDD, TDD是类似自然语言方式 BDD是结构主义 chai文旦地址 二.moc ...
- JavaScript 数组(Array)对象
1.Array相关的属性和方法 Array对象属性 constructor 返回对创建此对象的数组函数的引用: length 设置或返回数组中元素的数目: prototype 使您有能力向对象添加属性 ...
- 第一章:HTML5的基础
HTML5基础 1.DoctYpe声明 <!DCTYPE html>必须放在第一行. <title> <title> 百度</title> <me ...
- Python操作rabbitmq消息队列持久化
消息队列持久化 Python操作rabbit消息队列的持久化,如下: # 创建一个名为balance的队列,对queue进行durable持久化设为True(持久化第一步)channel.queue_ ...
- css垂直居中怎么设置?文字上下居中和图片垂直居中
css 居中分css垂直居中和css水平居中,水平居中平时比较常用,这里我们主要讲css上下居中的问题.垂直居中又分为css文字上下居中和css图片垂直居中,下面我们就分别来介绍一下. css文字上下 ...
- ES6的export与Nodejs的module.exports
原文:https://www.cnblogs.com/lxg0/p/7774094.html module.exports与exports,export与export default之间的关系和区别 ...
- C 表達式及返回值
以下程序的输出结果是__A____. #include<stdio.h> main() { ,j=; printf("%d,%d\n",++i,j--); } A., ...
- Java - Selenium 环境配置
1. 安装Java JDK - 文件自己下 2. 配置环境变量-重要! 我的电脑-属性-高级-环境变量 添加 CLASSPATH 值 .;%JAVA_HOME%\lib;%JAVA_HOME%\li ...