python3内部是使用unicode编码的。字符串在Python3内部的表示是unicode编码,相当于python2的u''格式。
因此在编码解码时,可以先在Python内部使用decode解码为unicode,然后使用encode进行编码。
decode的作用是将其他编码的字符串转换成unicode编码,如str1.decode('gb2312'),表示将gb2312编码的字符串str1转换成unicode编码。
encode的作用是将unicode编码转换成其他编码的字符串,如str2.encode('gb2312'),表示将unicode编码的字符串str2转换成gb2312编码。 (摘抄:https://blog.youkuaiyun.com/Amluee/article/details/78696047)
在学网络编程中遇到的:
解码:
s = socket.socket()
s.bind((host, port))
s.listen(5)
connection, address = s.accept()
request = connection.recv(1024)
print(request)
# b'GET / HTTP/1.1\r\nHost: localhost:2000\r\nConnection: keep-alive\r\ncontent-type:text/html;charset=UTF-8\r\n\r\n'
print(type(request))
# <class 'bytes'>
print(request.decode('utf-8'))
'''
GET / HTTP/1.1
Host: localhost:2000
Connection: keep-alive
content-type:text/html;charset=UTF-8
'''
# 这里 utf-8编码的bytes ---> unicode编码
# 通过decode解码,转码的时候要先搞明白str是什么编码,这里使用utf-8进行解码,我也不知道为什么是这个
编码:
def index(request):
head = 'HTTP/1.1 200 OK\r\n'
body = '<h1>Hello cc</h1>'
response = head + '\r\n' + body
# b'' 表示这是一个 bytes 对象
# 使用encode进行编码
# # 这里 unicode编码 ---> utf-8编码
return response.encode(encoding='utf-8')
# 下面的就不用再encode了
def index(request):
head = b'HTTP/1.1 200 OK\r\n'
with open('templates/index.html','rb') as f:
body = f.read()
response = head + b'\r\n' + body
return response
#如果返回的response .encode(encoding='utf-8'),
#那么会报错: 'bytes' object has no attribute 'encode'