Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I setup a local HTTP server using Python

Tags:

python

d3.js

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.

like image 596
sharky Avatar asked Jan 16 '15 05:01

sharky


People also ask

How do I run a local HTTP server?

Installing HTTP server using NPM Run the command line/terminal on your system (it doesn't matter which directory you are currently in). Execute npm install -g http-server . Once NPM finishes, you have the tiny HTTP-server installed. That's it.

How do I connect to an HTTP server in Python?

Accessing the Python HTTP Server Locally To access the server, open a browsing window and enter http://localhost:PORT_NUMBER into the URL field. If a port number is not specified in the previous step, the server will be found at http://localhost:8000 . From here, users can open or download any of the hosted files.

Does Python have HTTP server?

You can run python http server on any port, default port is 8000. Try to use port number greater than 1024 to avoid conflicts. Then open your favourite browser and type localhost:9000 .


2 Answers

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.html is the HTML file you wrote.

like image 31
Johnny Abou Haidar Avatar answered Oct 17 '22 03:10

Johnny Abou Haidar


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

like image 91
stvsmth Avatar answered Oct 17 '22 03:10

stvsmth