在学习《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. mysql帮助命令

    HELP contents 查看MySQL命令的使用. eg: HELP 'Data Type' 查看所有的数据类型的使用方法.

  2. Xib给特定view添加手势

    步骤1.拖拽手势注意:拖拽到First Responder下方,成功后会出现一个分类Objects(如图 拖拽成功后会多出一个分类Objects ) 步骤2.给需要的view绑定手势控件 拖拽gest ...

  3. [daily][centos][nginx] 在centos7使用nginx启用对文件目录的http访问

    1. 安装nginx yum install nginx 2. 修改配置 2.1 提供目录权限: 我需要访问的目录是 /home/data, 用户是data, 所以修改如下配置: [root@S205 ...

  4. 转:eclipse maven build、maven install 等区别

    原文地址:eclipse maven build.maven install 等区别

  5. LeetCode 929 Unique Email Addresses 解题报告

    题目要求 Every email consists of a local name and a domain name, separated by the @ sign. For example, i ...

  6. idea的基本使用

    对于Idea没有workspace的概念,但是它把每个项目都看成是maven的一个模块,在一个idea窗口要想显示多个项目时就和eclipse不太一样,下面会详细介绍. 另外maven的setting ...

  7. linux测试环境搭建步骤

    一.建用户 1.新建用户root用户登录,执行命令:useradd + 用户名 -m -d + 指定路径如:新建用户liuwq ,指定路径/home/ios命令:useradd liuwq -m -d ...

  8. Java中包装类型和基本类型的使用场景(什么时候使用包装类型)(转)

    说明:最简单的理解,基本类型有默认值,而包装类型初始为null.然后再根据这两个特性进行分业务使用,在阿里巴巴的规范里所有的POJO类必须使用包装类型,而在本地变量推荐使用基本类型. Java语言提供 ...

  9. mvc,EntityFramework调用分页存储过程

    此文讲述mvc4+entityframework6+sqlserver2008环境下调用存储过程,实现分页. 1.分页存储过程代码如下: 分页原理用的row_number()和over()函数实现(没 ...

  10. finecms如何调用多个指定栏目的内容

    想调用finecms多个栏目的内容,用英文状态下的逗号来分开多个id实现不了(catid=1,2,3),要如何写噢?后面ytkah想想这个跟sql语法有点一样,用IN_catid = 1,2,3果然成 ...