Python3 Http Web 服务器:虚拟主机

发布于 2024-07-17 13:01:12 字数 241 浏览 9 评论 0原文

我正在用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

荒岛晴空 2024-07-24 13:01:12

虚拟主机通过遵守 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

迷雾森÷林ヴ 2024-07-24 13:01:12

对于简单的 HTTP Web 服务器,您可以从 WSGI 参考实现:

wsgiref 是 WSGI 规范的参考实现,可用于向 Web 服务器或框架添加 WSGI 支持。 它提供了用于操作 WSGI 环境变量和响应标头的实用程序、用于实现 WSGI 服务器的基类、为 WSGI 应用程序提供服务的演示 HTTP 服务器,...

修改示例服务器以检查 HTTP_HOST< /code> 标头,这是一个简单的应用程序,它根据虚拟主机的不同,使用不同的文本进行响应。 (扩展示例以使用配置文件留作练习)。

import wsgiref
from wsgiref.simple_server import make_server

def my_app(environ,start_response):
    from io import StringIO
    stdout = StringIO()
    host = environ["HTTP_HOST"].split(":")[0]
    if host == "127.0.0.1":
        print("This is virtual host 1", file=stdout)
    elif host == "localhost":
        print("This is virtual host 2", file=stdout)
    else:
        print("Unknown virtual host", file=stdout)

    print("Hello world!", file=stdout)
    print(file=stdout)
    start_response(b"200 OK", [(b'Content-Type',b'text/plain; charset=utf-8')])
    return [stdout.getvalue().encode("utf-8")]

def test1():
    httpd = make_server('', 8000, my_app)
    print("Serving HTTP on port 8000...")

    # Respond to requests until process is killed
    httpd.serve_forever()

For a simple HTTP web server, you can start with the WSGI reference implementation:

wsgiref is a reference implementation of the WSGI specification that can be used to add WSGI support to a web server or framework. It provides utilities for manipulating WSGI environment variables and response headers, base classes for implementing WSGI servers, a demo HTTP server that serves WSGI applications,...

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).

import wsgiref
from wsgiref.simple_server import make_server

def my_app(environ,start_response):
    from io import StringIO
    stdout = StringIO()
    host = environ["HTTP_HOST"].split(":")[0]
    if host == "127.0.0.1":
        print("This is virtual host 1", file=stdout)
    elif host == "localhost":
        print("This is virtual host 2", file=stdout)
    else:
        print("Unknown virtual host", file=stdout)

    print("Hello world!", file=stdout)
    print(file=stdout)
    start_response(b"200 OK", [(b'Content-Type',b'text/plain; charset=utf-8')])
    return [stdout.getvalue().encode("utf-8")]

def test1():
    httpd = make_server('', 8000, my_app)
    print("Serving HTTP on port 8000...")

    # Respond to requests until process is killed
    httpd.serve_forever()
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文