如何使用 Python 设置本地 HTTP 服务器

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/27977972/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 02:33:23  来源:igfitidea点击:

How do I setup a local HTTP server using Python

pythond3.js

提问by sharky

I am trying to do some basic D3 programming. All the books I am reading talk about setting up a local http server and that is where I am finding myself stuck. I typed the following

我正在尝试做一些基本的 D3 编程。我正在阅读的所有书籍都在谈论设置本地 http 服务器,而这正是我发现自己陷入困境的地方。我输入了以下内容

python -m http.server 

to host the local server. Now, my problem is how to open my html file in this local server? I don't even know how to find the file in the command prompt. Any help will be appreciated. The following is my html file code on aptana. I also have put the d3.js file in the aptana.

托管本地服务器。现在,我的问题是如何在本地服务器中打开我的 html 文件?我什至不知道如何在命令提示符中找到该文件。任何帮助将不胜感激。以下是我在 aptana 上的 html 文件代码。我也把 d3.js 文件放在了 aptana 中。

<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="utf-8">
        <title>
            D3 Page Template
        </title>
        <script type="text/javascript" src="d3.js"></script>
    </head>
    <script type="text/javascript">
        //D3 codes will go here
    </script>
</html>

When I am running aptana, the html file is opening in a regular firefox page. I want it to open in the locally hosted http server page. Any hints.

当我运行 aptana 时,html 文件在常规的 Firefox 页面中打开。我希望它在本地托管的 http 服务器页面中打开。任何提示。

回答by stv

The answer is provided when you start the server. In the same directory where you have your HTML file, start the server:

启动服务器时会提供答案。在您拥有 HTML 文件的同一目录中,启动服务器:

$ python -m http.server
Serving HTTP on 0.0.0.0 port 8000 ...

(Or, the Python2 incantation)

(或者,Python2 咒语)

$ python -m SimpleHTTPServer
Serving HTTP on 0.0.0.0 port 8000 ...

In this message, Python tells you the IP address (0.0.0.0) and the port number (8000).

在此消息中,Python 会告诉您 IP 地址 (0.0.0.0) 和端口号 (8000)。

So, if the file is named d3_template.html, you can get to this page via http://0.0.0.0:8000/d3_template.html

因此,如果文件名为 d3_template.html,您可以通过 http://0.0.0.0:8000/d3_template.html

On most machines you should also be able to use

在大多数机器上,您还应该能够使用

http://localhost:8000/d3_template.htmlor http://127.0.0.1:8000/d3_template.html

http://localhost:8000/d3_template.html或者 http://127.0.0.1:8000/d3_template.html

If you get an error like this:

如果你得到这样的错误:

socket.error: [Errno 48] Address already in use

socket.error: [Errno 48] Address already in use

You want to use a different port:

您想使用不同的端口:

$ python -m http.server 8888

$ python -m http.server 8888

And to load the file:

并加载文件:

http://0.0.0.0:8888/d3_template.html

http://0.0.0.0:8888/d3_template.html

To understand why all of these work, you'd want to learn a fair bit about networking (ports, DNS, loopback interface, how multiple network cards behave on the same machine and, if things aren't working as expected, firewalls, restricted ports and who knows what else).

要了解所有这些工作的原因,您需要了解一些有关网络的知识(端口、DNS、环回接口、多个网卡在同一台机器上的行为方式,以及如果事情没有按预期工作,防火墙、受限端口,谁知道还有什么)。

回答by VanillaSpinIce

I've created a small portable python 3 script (should work on MacOS/Linux) to locally render html file that use d3 or more generally websites. I thought this could be useful for others.

我创建了一个小型便携式 python 3 脚本(应该在 MacOS/Linux 上工作)来本地渲染使用 d3 或更一般网站的 html 文件。我认为这可能对其他人有用。

Essentially it creates a local server using a subprocess, opens your browser for rendering and then shuts down the server properly for fast reuse. You can find the Python 3 script here (with some detail on how to use it): https://github.com/alexandreday/local_server. An example use is:

本质上,它使用子进程创建本地服务器,打开浏览器进行渲染,然后正确关闭服务器以快速重用。您可以在此处找到 Python 3 脚本(包含有关如何使用它的一些详细信息):https: //github.com/alexandreday/local_server。一个示例使用是:

$ python custom_server.py index.html

This will render your index.html file which uses d3.js or a website more generally.

这将呈现使用 d3.js 或更一般的网站的 index.html 文件。

回答by Johnny Abou Haidar

Try this:

尝试这个:

from http.server import HTTPServer, BaseHTTPRequestHandler

class Serv(BaseHTTPRequestHandler):

def do_GET(self):
    if self.path == '/':
        self.path = '/test.html'
    try:
        file_to_open = open(self.path[1:]).read()
        self.send_response(200)
    except:
        file_to_open = "File not found"
        self.send_response(404)
    self.end_headers()
    self.wfile.write(bytes(file_to_open, 'utf-8'))


httpd = HTTPServer(('localhost',8080),Serv)
httpd.serve_forever()

Where test.htmlis the HTML file you wrote.

test.html你写的 HTML 文件在哪里。