Last active 1440821902

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.

ipinfo.py Raw
1# ipinfo.py by Steven Smith (blha303), MIT license (as is every script on my gist, by the way, unless otherwise stated)
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
5from flask import Flask, jsonify, redirect
6import socket
7app = Flask(__name__)
8ip_addrs = None
9
10def 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
15def 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("/")
24def getip():
25 update()
26 return jsonify(ip_addrs=ip_addrs)
27
28@app.route("/<path:path>")
29def path(path):
30 update()
31 return redirect("http://{}:{}".format(select(), path), code=307)
32
33if __name__ == "__main__":
34 update()
35 app.run(port=5212, debug=True)