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 ...
随机推荐
- jdbc实现分页,需要前端传当前页码
1.封装一个公共实体类用于返回:实体数据,当前页,总页数,总条数,每页多少条 public class PageInfo<T> { //一页显示的记录数 private int numPe ...
- [dpdk] dpdk启动几个线程
看别人的代码搞得有点晕,突然有点不确定,再确认一次. 使用 helloworld程序测试一下. /root/dpdk-16.07/examples/helloworld 一: 只启动一个核心. [r ...
- [potatos][flex][TBC] 语义分析词法分析 flex
FLEX: The Fast Lexical Analyzer https://github.com/westes/flex 这并不是我的人生中第一次遇见flex,好多工程中,我都发现他们用到了fle ...
- [archlinux][daily] 自建DNS服务器 / 建立本地DNS cache / 使用dnsmasq加速上网
新公司,上网超慢,DNS竟然是远程地址,终于找到机会学习一下dnsmasq了. update@20170516: 上网慢是因为分给我的IP有限流策略,其实远端DNS并不会造成感受上的上网慢. 参考:h ...
- spring相关的maven依赖
<properties> <springframework.version>5.0.4.RELEASE</springframework.version> < ...
- shiro默认过滤器
- vue脚手架用axios请求本地数据
首先需要声明的是:本地请求,不用考虑跨域问题,这适用刚入坑的前端小白看,小白在做自己的项目时,通常都是用自己写的json数据,之后用axios请求过来,渲染到页面上. 1.cnpm install a ...
- glove理解
先贴个看起来讲的不错的文章链接,后续详细看了再补充https://blog.csdn.net/leyounger/article/details/78206832 2018.10.28大概了解了下gl ...
- 帝国cms修改栏目后文章列表的url错误怎么解决
修改了某个栏目的目录地址,原本是/abc/,现在改成了/ab/,重新生成了栏目页面/ab/和文章页面/ab/*.html,但是栏目页的列表文章url还是/abc/*.html(正确的url应该是/ab ...
- HBase 的MOB压缩分区策略介绍
版权声明:本文为博主原创文章.未经博主同意不得转载. https://blog.csdn.net/zNZQhb07Nr/article/details/79832392 HBase应用场景很广泛.社区 ...