如何使用Python设置本地HTTP服务器 [英] How do I setup a local HTTP server using Python

查看:336
本文介绍了如何使用Python设置本地HTTP服务器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

python -m http.server 

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

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.

<!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>

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

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.

推荐答案

启动服务器时将提供答案.在您拥有HTML文件的目录中,启动服务器:

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

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

(或者,Python2的咒语)

(Or, the Python2 incantation)

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

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

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

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

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

在大多数计算机上,您也应该能够使用

On most machines you should also be able to use

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

如果出现这样的错误:

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

您要使用其他端口:

$ python -m http.server 8888

并加载文件:

http://0.0.0.0:8888/d3_template.html

要了解所有这些功能为何起作用,您需要学习一些有关网络的知识(端口,DNS,环回接口,多个网卡在同一台计算机上的行为方式,以及如果运行不正常,防火墙,受限制的端口,还有谁知道呢.)

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

这篇关于如何使用Python设置本地HTTP服务器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆