在学习《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'的更多相关文章

  1. linux下运行python3出现TypeError: a bytes-like object is required, not 'str'

    目标:用python将中文存入csv,且中文正常显示. 环境:linux,python3 百度N久,方法都不行或是比较复杂. 以上代码用python3运行后,出现TypeError: a bytes- ...

  2. TCP客户端和服务器间传输数据遇到的TypeError: a bytes-like object is required, not 'str'问题

    使用python实现python核心编程3第472页和474页的TCP时间戳服务器和客户端服务器间数据传输编程时遇到TypeError: a bytes-like object is required ...

  3. 【爬坑】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 ...

  4. moviepy执行TextClip.search方法时报错TypeError: a bytes-like object is required, not str

    ☞ ░ 前往老猿Python博文目录 ░ 执行TextClip.search方法时,报错: >>> from moviepy.editor import * >>> ...

  5. 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 ...

  6. 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 ...

  7. 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 ...

  8. TypeError: a bytes-like object is required, not 'str'

    python bytes和str两种类型转换的函数encode(),decode() str通过encode()方法可以编码为指定的bytes 反过来,如果我们从网络或磁盘上读取了字节流,那么读到的数 ...

  9. python 3.5: TypeError: a bytes-like object is required, not 'str'

    出现该错误往往是通过open()函数打开文本文件时,使用了'rb'属性,如:fileHandle=open(filename,'rb'),则此时是通过二进制方式打开文件的,所以在后面处理时如果使用了s ...

随机推荐

  1. Entity Framework 6如何进行导航属性的筛选(context.Msg.First(t=>t.Id==1).Include(t=>t.MsgDetail),筛选MsgDetail带条件)

    问题: https://q.cnblogs.com/q/98333/ Msg表(Id,Content,IsDel).内有 virtual ICollection<MsgDetail> Ms ...

  2. dispatch_barrier_async,加锁

    dispatch_barrier_async用于文件读写,在写的时候,不允许进行读操作,在写操作完成才能进行读取操作,为了保证线程安全 加锁的方式: 1. barrier 2. NSLock 3. @ ...

  3. python摸爬滚打之day07----基本数据类型补充, 集合, 深浅拷贝

    1.补充 1.1  join()字符串拼接. strs = "阿妹哦你是我的丫个哩个啷" nw_strs = "_".join(strs) print(nw_s ...

  4. 第5章 实现windows程序的数据绑定

    1:连接式: command 对象负责对数据库的执行命令 DataReader 对象负责从数据源中读取数据 connection 对象负责链接数据库 断开式: 数据集的Dataset存放在独立的数据源 ...

  5. 第二节:Linux下检出项目和提交项目

    1.在linux下获取项目: 2.在linux中提交项目: 这时候我们在光标处添加注释,然后保存退出. 再查看下状态: 接下来问题来了: 解决办法: vim .git/config 修改:[remot ...

  6. 【SQL】如何使用SQL like 方法和SQL [charlist] 通配符(SQL like的拓展)

    1.like 相关用法 '%a'    //以a结尾的数据 'a%'    //以a开头的数据 '%a%'    //含有a的数据 ‘_a_’    //三位且中间字母是a的 '_a'    //两位 ...

  7. OC动画:CAAnimationGroup

    //贝塞尔曲线路径 UIBezierPath *movePath = [UIBezierPath bezierPath]; [movePath moveToPoint:CGPointMake(10.0 ...

  8. 20165236 实验二 《Java面向对象程序设计》实验报告

    20165236 实验二<Java面向对象程序设计>实验报告 姓名:郭金涛       学号:20165236      课程:Java程序设计 指导老师:娄嘉鹏       实验时间:2 ...

  9. web页面判断是否首次加载

    判断web页面是否是首次加载: if(!window.name){ window.name ='name' this.setState({ note:true })}

  10. GENIL_BOL_BROWSER, GENIL_MODEL_BROWSER,BSP_WD_CMPWB 使用方法

    一:GENIL_BOL_BROWSER 使用方法 1: 进入x3c系统.输入T-CODE  GENIL_BOL_BROWSER 2: 输入一个component set  名称 3: 选择一个对象,双 ...