ipinfo.py
· 1.1 KiB · Python
Raw
# ipinfo.py by Steven Smith (blha303)
# A local service to get your ip, for if you have a local service running that you want to link to other people but you can't be bothered finding your IP first.
# I use it for Plex, with a bookmark pointing to http://localhost:5212/32400/web/index.html
from flask import Flask, jsonify, redirect
import socket
app = Flask(__name__)
ip_addrs = None
def update():
global ip_addrs
ip_addrs = [ip for ip in socket.gethostbyname_ex(socket.gethostname())[2]
if not ip[:4] == "127."]
def select():
global ip_addrs
ips = [s for s in ip_addrs if s[:8] == "192.168."] + [s for s in ip_addrs if s[:4] == "10."] + [s for s in ip_addrs if s[:7] == "172.16."]
if ips:
return ips[0]
else:
return ip_addrs[0]
@app.route("/")
def getip():
update()
return jsonify(ip_addrs=ip_addrs)
@app.route("/<path:path>")
def path(path):
update()
return redirect("http://{}:{}".format(select(), path), code=307)
if __name__ == "__main__":
update()
app.run(port=5212, debug=True)
| 1 | # ipinfo.py by Steven Smith (blha303) |
| 2 | # A local service to get your ip, for if you have a local service running that you want to link to other people but you can't be bothered finding your IP first. |
| 3 | # I use it for Plex, with a bookmark pointing to http://localhost:5212/32400/web/index.html |
| 4 | |
| 5 | from flask import Flask, jsonify, redirect |
| 6 | import socket |
| 7 | app = Flask(__name__) |
| 8 | ip_addrs = None |
| 9 | |
| 10 | def update(): |
| 11 | global ip_addrs |
| 12 | ip_addrs = [ip for ip in socket.gethostbyname_ex(socket.gethostname())[2] |
| 13 | if not ip[:4] == "127."] |
| 14 | |
| 15 | def select(): |
| 16 | global ip_addrs |
| 17 | ips = [s for s in ip_addrs if s[:8] == "192.168."] + [s for s in ip_addrs if s[:4] == "10."] + [s for s in ip_addrs if s[:7] == "172.16."] |
| 18 | if ips: |
| 19 | return ips[0] |
| 20 | else: |
| 21 | return ip_addrs[0] |
| 22 | |
| 23 | @app.route("/") |
| 24 | def getip(): |
| 25 | update() |
| 26 | return jsonify(ip_addrs=ip_addrs) |
| 27 | |
| 28 | @app.route("/<path:path>") |
| 29 | def path(path): |
| 30 | update() |
| 31 | return redirect("http://{}:{}".format(select(), path), code=307) |
| 32 | |
| 33 | if __name__ == "__main__": |
| 34 | update() |
| 35 | app.run(port=5212, debug=True) |