Python - HTTP servers

Python http.server模块搭建HTTP服务器

python的http.server模块用于HTTP服务器的功能,这个模块是python标准库的一部分,不需要pip install。

使用前需要import:

import http.server

然后就可以编辑代码,使用此模块提供的接口,实现http server相关功能。

除此之外,在命令行里直接通过模块方式运行,也可以启动一个http服务器。说明如下。

可以使用解释器的 -m 开关直接调用 http.server。这将为相对于当前目录的文件提供服务:

http.server can be invoked directly using the -m switch of the interpreter. This serves files relative to the current directory:

python -m http.server

服务器默认监听 8000 端口。可以通过将所需的端口号作为参数来覆盖默认值:

The server listens to port 8000 by default. The default can be overridden by passing the desired port number as an argument:

python -m http.server 9000

默认情况下,服务器会将自己绑定到所有接口。选项 -b/--bind 可指定服务器应绑定的特定地址。支持 IPv4 和 IPv6 地址。例如,以下命令会使服务器只绑定到 localhost:

By default, the server binds itself to all interfaces. The option -b/--bind specifies a specific address to which it should bind. Both IPv4 and IPv6 addresses are supported. For example, the following command causes the server to bind to localhost only:

python -m http.server --bind 127.0.0.1

3.4 版中更改: 添加了 --bind 选项。

3.8 版中更改: 在 --bind 选项中支持 IPv6。

Changed in version 3.4: Added the --bind option.

Changed in version 3.8: Support IPv6 in the --bind option.

默认情况下,服务器使用当前目录。选项-d/--directory指定了服务器提供文件的目录。例如,以下命令使用了一个特定目录:

By default, the server uses the current directory. The option -d/--directory specifies a directory to which it should serve the files. For example, the following command uses a specific directory:

python -m http.server --directory /tmp/

3.7 版中更改:添加了 --directory 选项。

Changed in version 3.7: Added the --directory option.

默认情况下,服务器符合 HTTP/1.0。选项 -p/--protocol 指定了服务器所遵从的 HTTP 版本。例如,下面的命令运行一个 HTTP/1.1 兼容服务器:

By default, the server is conformant to HTTP/1.0. The option -p/--protocol specifies the HTTP version to which the server is conformant. For example, the following command runs an HTTP/1.1 conformant server:

python -m http.server --protocol HTTP/1.1

3.11 版中更改:添加了 --protocol 选项。

Changed in version 3.11: Added the --protocol option.

在浏览器中输入IP地址和端口号,就能够访问HTTP服务器了。

参考:

http.server — HTTP servers — Python 3.12.7 documentation

在比较 `python-kafka` 和 `confluent-kafka` 这两个 Python 客户端库时,可以从功能、性能、易用性和生态系统支持等多个维度进行分析。 ### 功能支持 `python-kafka` 是一个纯 Python 实现的 Kafka 客户端,支持 Kafka 的基本功能,包括生产者(Producer)、消费者(Consumer)和管理操作(如 topic 创建和删除)[^1]。它与 Kafka 的 Java 客户端相比功能较为基础,但在某些场景下已经足够使用。 `confluent-kafka` 则是基于 C 语言实现的 Kafka 客户端库(librdkafka),提供了 Python 的绑定。它不仅支持 Kafka 的核心功能,还集成了 Confluent 提供的许多高级特性,如 Schema Registry 支持、Kafka Connect 集成以及 Kafka Streams 的流处理能力。 ### 性能 由于 `confluent-kafka` 是基于 C 语言实现的 librdkafka,其性能通常优于 `python-kafka`。特别是在高吞吐量和低延迟的场景下,`confluent-kafka` 表现出更高的效率和更低的资源消耗。而 `python-kafka` 作为纯 Python 实现,在性能上相对较弱,尤其是在处理大量消息时可能会出现瓶颈。 ### 易用性 `python-kafka` 的 API 设计较为简单,适合快速上手和使用。对于只需要基本 Kafka 功能的应用来说,`python-kafka` 提供了足够的灵活性和易用性。 相比之下,`confluent-kafka` 的 API 更加复杂,但同时也提供了更多的配置选项和高级功能。虽然学习曲线较陡,但对于需要高性能和复杂功能的应用来说,`confluent-kafka` 是更好的选择。 ### 生态系统支持 `confluent-kafka` 与 Confluent 平台紧密集成,支持 Schema Registry、Kafka Connect 和 Kafka Streams 等高级功能。这些工具可以帮助开发者更好地管理和处理 Kafka 中的数据流。此外,Confluent 还提供了丰富的文档和技术支持,使得 `confluent-kafka` 在企业级应用中更具优势。 `python-kafka` 虽然也有一定的社区支持,但在生态系统方面不如 `confluent-kafka` 强大。它更适合于那些只需要 Kafka 基本功能的小型项目或原型开发。 ### 示例代码 以下是一个简单的生产者示例,展示如何使用 `python-kafka` 发送消息: ```python from kafka import KafkaProducer producer = KafkaProducer(bootstrap_servers='localhost:9092') producer.send('my-topic', b'Hello, Kafka!') producer.flush() producer.close() ``` 而 `confluent-kafka` 的生产者示例如下: ```python from confluent_kafka import Producer conf = {'bootstrap.servers': 'localhost:9092'} producer = Producer(conf) producer.produce('my-topic', key='key', value='Hello, Confluent Kafka!') producer.flush() producer.close() ``` ### 总结 - **功能**:`confluent-kafka` 提供了更丰富的功能和集成支持。 - **性能**:`confluent-kafka` 在性能上明显优于 `python-kafka`。 - **易用性**:`python-kafka` 更加简单易用,适合快速开发。 - **生态系统**:`confluent-kafka` 与 Confluent 平台集成,生态系统更强大。 根据具体需求和应用场景,可以选择合适的 Kafka Python 客户端库。如果需要高性能和高级功能,推荐使用 `confluent-kafka`;如果只需要基本功能且希望快速上手,则可以考虑 `python-kafka`。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

夜流冰

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值