Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I host a localhost on my server with Mac Terminal?

I've been hosting a localhost on my Mac with CSS, HTML, and JS. To do this i just navigate to my file with cd Desktop followed by cd filename , and then I do python -m SimpleHTTPServer 8000 to host my server on my localhost. I know that this only works for the person hosting the server, but I'd like to host it on my local network, so anyone that goes to localhost:8000 will see it. (I'm fine with it not being localhost:8000, in fact, I'd love a custom name.)
Thank you
-A

like image 913
AHolasek Avatar asked Oct 24 '25 04:10

AHolasek


1 Answers

First of all, localhost is a "domain" name if you like. Most of the times it resolves to 127.0.0.1 which is the loopback ip address(e.g. points back to your computer). I am going to assume you are using python 2.x

So here we go:

#!/usr/bin/env python
import BaseHTTPServer
from SimpleHTTPServer import SimpleHTTPRequestHandler

addr = ("0.0.0.0", 8000) #host to everyone

serv = BaseHTTPServer.HTTPServer(addr, SimpleHTTPRequestHandler)

serv.serve_forever()

Save that to a python script and run with:

python myfile.py

If you are using python 3 then go with :

python3 -m http.server --bind 0.0.0.0 8000

Now for someone else to access your server through your local network, you have to give them your machine's ip. To do that run:

ifconfig |grep inet

You should get something alone the lines of:

inet 192.168.1.2 netmask 0xffffff00 etc etc

Now anyone on your local network can use your server by typing

192.168.1.2:8000 

in their browsers

like image 74
George TG Avatar answered Oct 26 '25 17:10

George TG