Python3 Http Web 服务器:虚拟主机
我正在用 python3 编写一个相当简单的 http Web 服务器。 Web 服务器需要简单 - 只需从配置文件等进行基本读取。我只使用标准库,目前它工作得相当不错。
这个项目只有一个需求,我自己无法实现——虚拟主机。 我需要至少有两个在配置文件中定义的虚拟主机。 问题是,我找不到如何在 python 中实现它们的方法。 有谁有任何指南、文章,也许有一些简单的实现,如何做到这一点?
我将不胜感激任何帮助。
I am writing an rather simple http web server in python3. The web server needs to be simple - only basic reading from config files, etc. I am using only standard libraries and for now it works rather ok.
There is only one requirement for this project, which I can't implement on my own - virtual hosts. I need to have at least two virtual hosts, defined in config files. The problem is, that I can't find a way how can I implement them in python. Does anyone have any guides, articles, maybe some simple implementation how can this be done?
I would be grateful for any help.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
虚拟主机通过遵守 HTTP 请求中的 Host: 标头来工作。
只需读取请求的标头,并根据 Host: 标头的值采取行动
Virtual hosts work by obeying the Host: header in the HTTP request.
Just read the headers of the request, and take action based on the value of the Host: header
对于简单的 HTTP Web 服务器,您可以从 WSGI 参考实现:
修改示例服务器以检查
HTTP_HOST< /code> 标头,这是一个简单的应用程序,它根据虚拟主机的不同,使用不同的文本进行响应。 (扩展示例以使用配置文件留作练习)。
For a simple HTTP web server, you can start with the WSGI reference implementation:
Modifying the example server to check the
HTTP_HOST
header, here is a simple app that responds, depending on the virtual host, with a different text. (Extending the example to use a configuration file is left as an exercise).