搭建简易 web 服务器过程中的问题

简述500 lines or less 是一本在Github上开源的Python教程书籍,内容包含有18个Python Project。正如书名,都是低于500行代码的Project。

  不过,项目是基于Python2的,而我自学的是Python3,因此编写过程中遇到了2个问题。


1 库名变化

1.1 问题

  原Python2程序中导入了库 BaseHTTPServer ,但在Python3中,该库已经改为 http.server

1.2 解决办法

  import BaseHTTPServer —> import http.server

  就是这样,而已。


# 2 网页编码

2.1 问题

  原Python2程序中有一段类似如下的html代码,

1
2
3
4
5
6
7
Page = """\
<html>
<body>
<p>Hello, web!</p>
</body>
</html>
"""

  但运行时出现 TypeError: a bytes-like object is required, not 'str'

2.2 解决办法

  解决办法有两种,一种是使用前缀 b 将str转化为bytes;另一种是使用 str.encode('utf-8')

1
2
3
4
5
6
7
Page = b"""\
...all the other code...
"""
或者
Page = """\
...all the other code...
""".encode('utf-8')

  我在StackOverFlow上找到相关解释,大概意思是:在Python3中字符串是Unicode形式,但在网络中,数据应该是以字节的形式传输。

The reason for this error is that in Python 3, strings are Unicode, but when transmitting on the network, the data needs to be bytes strings instead. So… a couple of suggestions:

……

Best solution:

1
2
output = 'Thank you for connecting'
c.sendall(output.encode('utf-8'))


以上!

作者

Alex

发布于

2018-05-27

更新于

2021-06-19

许可协议

评论