Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
384 views
in Technique[技术] by (71.8m points)

d3.js - How do I setup a local HTTP server using Python

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 

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>

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

(Or, the Python2 incantation)

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

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 or http://127.0.0.1:8000/d3_template.html

If you get an error like this:

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

You want to use a different port:

$ python -m http.server 8888

And to load the file:

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...