Python+client_connection.sendall(http_resp)+TypeError: 'str' does not support the buffer interface
来源:互联网 发布:linux gzip 解压 编辑:程序博客网 时间:2024/06/11 14:24
此文记录本人学习进步点滴,如有错误或不妥,请不吝赐教。
在使用python搭建一个简单的web server时遇到的问题,代码如下
import socketHOST, PORT = '', 8888listen_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)listen_socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)listen_socket.bind((HOST, PORT))listen_socket.listen(1)print('Serving HTTP on port '+str(PORT)+'...')while True: client_connection, client_address = listen_socket.accept() request = client_connection.recv(1024) print(request) http_resp = """\HTTP/1.1 200 OKHello, World!""" client_connection.sendall(http_resp) client_connection.close()
错误信息表明这是一个类型错误。在stackoverflow上寻找解决方案,发现这是在把python 2版本的代码移植到python 3版本时容易发生的错误。
在python 3中,bytes strings 和 unicode strings是两个不同的类型。我们知道,在网络通信的socket中,使用的是字节流,即bytes strings,而程序中的http_resp是unicode string,所以会发生TypeError,因为socket使用的是字节流buffer。
幸而,python提供了bytes strings和unicode strings之间进行转换的机制。使用unicode strings的encode()可以将其转为bytes strings;使用bytes strings的decode()可以将其转为unicode strings。
好,我们尝试一下,将原代码中的client_connection.sendall(http_resp)改为client_connection.sendall(http_resp.encode()),运行脚本,可正常运行
接着我们打开浏览器,访问localhost:8888/hello,能正常访问
我们知道,python中的bytes strings定义需在前面附上字符b,所以我们尝试另一种解决方案,修改http_resp定义为
http_response = b"""\HTTP/1.1 200 OKHello, World!"""
同样能正常运行脚本和访问localhost:8888/hello,所以可知问题确实是由bytes strings和unicode strings引起的。
- Python+client_connection.sendall(http_resp)+TypeError: 'str' does not support the buffer interface
- Python 3 TypeError: ‘str’ does not support the buffer interface
- TypeError:'str' does not support the buffer interface
- TypeError:’str’ does not support the buffer interface
- TypeError: 'str' does not support the buffer interface
- python socket send 错误:TypeError: 'str' does not support the buffer interface
- Python List find方法报错 TypeError: 'str' does not support the buffer interface
- Python笔记002——错误TypeError: 'str' does not support the buffer interface
- python ‘str’ does not support the buffer interface解决办法
- ‘str' does not support the buffer interface
- Python 3中套接字编程中遇到TypeError: 'str' does not support the buffer interface的解决办法
- Python 3中套接字编程中遇到TypeError: 'str' does not support the buffer interface的解决办法
- Python 3中套接字编程中遇到TypeError: 'str' does not support the buffer interface的解决办法
- Python 3中套接字编程中遇到TypeError: 'str' does not support the buffer interface的解决办法
- Python 3中套接字编程中遇到TypeError: 'str' does not support the buffer interface的解决办法
- python3中split分割字符串出现错误 TypeError‘str’ does not support the buffer interface
- Python_bug收集_TypeError:'str' does not support the buffer interface
- python3 ‘str’ does not support the buffer interface解决方案
- 创建 event_base
- 搭建匿名FTP
- -weak
- HasnMap中的键
- 探讨android更新UI的几种方法
- Python+client_connection.sendall(http_resp)+TypeError: 'str' does not support the buffer interface
- mac环境python Flask 环境搭建
- linux 添加用户、权限
- ios报错--pie can only be used when targeting iOS 4.2 or later clang: error: linker command failed with
- Linux 常用命令详解
- 让程序员跳槽的非钱原因
- Helloworld
- HDU 3342 Legal or Not
- poj 2229 dp(分解为2的幂)