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 ...
随机推荐
- word标题自动编号
1.打开word文档中多级列表->定义新的多级列表 2.根据下图设置级别对应的标题,然后确定
- eclipse debug调试时老是被URLClassLoader这个类拦截到,不能进入到要调试的类里面去
在使用eclipse进行试的时候,一直进入到URLClassLoader,而不能正常的进入断点,后来经过查资料,解决方法如下: 上面是百度给出的答案,我把图贴在这里,以便以后其他组的朋友遇到这个问题的 ...
- Python3学习之路~4.4 软件目录结构规范
为什么要设计好目录结构? 可读性高: 不熟悉这个项目的代码的人,一眼就能看懂目录结构,知道程序启动脚本是哪个,测试目录在哪儿,配置文件在哪儿等等.从而非常快速的了解这个项目. 可维护性高: 定义好组织 ...
- adb移动端测试
1. Android介绍 Android 是google公司主导的一个开放的手机操作系统,不过目前已经超过了手机的局限,而定位于移动设备的操作系统. Android一词的本义指“机器人”,取名原因很简 ...
- 解决ios10以上点击缩放的问题
禁止ios10以上点击缩放,代码如下: <script> window.onload=function () { document.addEventListener('touchstart ...
- python的__all__
用来暴露接口 控制 from xxx import * 的行为 代码中当然是不提倡用 from xxx import * 的写法的,但是在 console 调试的时候图个方便还是很常见的.如果一个模块 ...
- logging日志模块的使用
logging日志模块的使用 logging模块中有5个日志级别: debug 10 info 20 warning 30 error 40 critical 50 通常使用日志模块,是用字典进行配置 ...
- 使用Python自带difflib模块进行文件内容差异对比
difflib_text.py #!/usr/bin/python import difflib import sys try: textfile1=sys.argv[1] textfile2=sys ...
- openshift 容器云从入门到崩溃之三《安装openshift》
准备好环境,在安装之前请先了解openshift提供的ansible有大量的安装选项 文档地址:https://docs.okd.io/latest/install/configuring_inven ...
- [LeetCode] 709. To Lower Case_Easy
Implement function ToLowerCase() that has a string parameter str, and returns the same string in low ...