Steven Smith revised this gist . Go to revision
1 file changed, 1 insertion, 1 deletion
ipinfo.py
| @@ -1,4 +1,4 @@ | |||
| 1 | - | # ipinfo.py by Steven Smith (blha303) | |
| 1 | + | # ipinfo.py by Steven Smith (blha303), MIT license (as is every script on my gist, by the way, unless otherwise stated) | |
| 2 | 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 | 3 | # I use it for Plex, with a bookmark pointing to http://localhost:5212/32400/web/index.html | |
| 4 | 4 | ||
Steven Smith revised this gist . Go to revision
1 file changed, 35 insertions
ipinfo.py(file created)
| @@ -0,0 +1,35 @@ | |||
| 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) | |