python3编码

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.csdn.net/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'
    

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值