title

How to Run HTML File on Localhost

IIRC macOS comes with PHP preinstalled, and PHP has built-in web-server which should be enough for serving static content.

So, open Terminal.app and then:

cd your/project/dir
php -S localhost:8080

After than you can navigate to http://localhost:8080/ and see your site in the browser (given you have index.html in your project, otherwise there will be «Not Found» message).

There are more advanced and/or less terminal-oriented ways, of course, but since you already tinkering with python and node, another terminal command should not be a problem.

BTW, you might want to look at that terminal window from time to time, as it outputs nice log of what things were requested from server. Good if you want to check for invalid references, 404 errors, etc. Here is a sample output:

$ php -S localhost:8080
PHP 7.3.6 Development Server started at Sat Jun 22 20:00:28 2019
Listening on http://localhost:8080
Document root is /private/tmp/test
Press Ctrl-C to quit.
[Sat Jun 22 20:00:32 2019] [::1]:51640 [200]: /
[Sat Jun 22 20:00:32 2019] [::1]:51641 [200]: /style.css
[Sat Jun 22 20:02:35 2019] [::1]:51670 [404]: /oops.html - No such file or directory

As you can see, root folder ( / , which was translated to index.html in my case) and a stylesheet ( style.css ) were requested and successfully delivered (code is 200 ). But non-existent file oops.html resulted in error (code is 404 ).

Читайте также:  Python numbers strings and lists

Running HTML file in localhost:8080

You need to add an option that’ll put your website on port 8080 because the http.server command defaults to port 8000 .

python3 -m http.server 8080

Then when you go to 0.0.0.0:8080 it should show you your webpage instead of a download prompt.

Also, you might have another instance of http.server running on port 8080 .

You can find the PID of this task using:

Which should show something that looks like this:

Command Output

Then you could kill it using:

Or in my case the task that’s running on port 8080 is:

Or, if you don’t mind, just kill all Python3 tasks using:

Which in my case would kill both Python3 tasks.

WARNING: be very, VERY careful before running the killall command, because this command will NOT save your work.

UPDATE: that blurry section in the picture is my username, I wasn’t sure if it would be against the rules to include it.

How to host a local html file in localhost using python alone

if you are using python3, go to working directory where the files are located.
Then run the following from terminal

now open a browser and navigate to localhost:8000

python -m SimpleHTTPServer 8000

How to display HTML using Python Alone in localhost

There’s way to do so using the python web frameworks like Flask or Django . In a typical flask scenario your code would look like this:-

2) Write your code.py like this:-

from flask import Flask, url_for
from flask import render_template

app = Flask(__name__)

@app.route('/')
def index():
return render_template('hello.html')

3) Next create a templates folder inside which put your html file which I have named it to be hello.html

templates > hello.html













Home
Contact