Are you making your web server in Python through which framework? I'll assume you are using Flask. So the basic setup is to install the dependencies. In my case I'll install pip install flask netifaces
. You should tweak my code to make it work with your web server app. I hope this works and fit your needs.
from flask import Flask
import netifaces
import socket
app = Flask(__name__)
# a) search for the best ip to show
def get_best_ip():
candidates = []
for iface in netifaces.interfaces():
addrs = netifaces.ifaddresses(iface)
if netifaces.AF_INET in addrs:
for addr in addrs[netifaces.AF_INET]:
ip = addr['addr']
if ip.startswith('127.') or ip.startswith('169.254.'):
continue # skip loopback and link-local
candidates.append(ip)
# prefer private LAN IPs
for ip in candidates:
if ip.startswith('192.168.') or ip.startswith('10.') or ip.startswith('172.'):
return ip
if candidates:
return candidates[0]
return 'localhost' # localhost fallback
# b) the app itself
@app.route('/')
def index():
return '''
<h1>hello</h1>
<p>replace this code with ur actual website</p>
<p>try accessing this page from another device on the same Wi-Fi using the IP address shown in your terminal</p>
'''
# c) start server + show IP
if __name__ == '__main__':
port = 8080
ip = get_best_ip()
print("Web interface available at:")
print(f"http://{ip}:{port}")
print("You can open the link above on any device on the same network.")
print("You can only open the link below on this device.")
print("http://127.0.0.1:8080")
app.run(host='0.0.0.0', port=port)