Add README and code for pubIp

This commit is contained in:
Viktor Varland 2016-02-28 14:21:43 +01:00
commit 010336fad8
3 changed files with 79 additions and 0 deletions

3
.gitignore vendored Normal file
View file

@ -0,0 +1,3 @@
*.py
*.pyc
*.swp

17
README.md Normal file
View file

@ -0,0 +1,17 @@
pubIp
=====
Simple dynamic DNS resolver which works with
[Tomato](http://www.polarcloud.com/tomato) and requires [Python
3](http://python.org).
## `/`:
Shows the current IP that is set
## `/update/:ip`:
Updates the current IP.
`:ip` must match the request client address.

59
pubip.py Normal file
View file

@ -0,0 +1,59 @@
#!/usr/bin/python3
import http.server
import socket
class pubIpServer(http.server.HTTPServer):
current_ip = 'n/a'
class pubIpHandler(http.server.BaseHTTPRequestHandler):
def do_GET(self):
self.send_response(200)
self.send_header('Content-type','text/html')
self.end_headers()
# Send the html message
c_address, c_port = self.client_address
s_address = self.server.current_ip
path = self.path.split('/')
if path[1] == '':
msg = s_address
elif path[1] == 'update':
n_address = path[2]
try:
socket.inet_aton(n_address)
except socket.error:
print('Error on IP conversion: {}'.format(n_address))
return
if n_address == s_address:
print('IP was identical, skipping...')
return
if n_address == c_address:
print('Updating dynamic IP: {}'.format(n_address))
self.server.current_ip = n_address
else:
print('Update IP mismatch: {}, {}'.format(c_address, n_address))
return
msg = s_address
else:
msg = ''
self.wfile.write(msg.encode('utf-8'))
return
def run(server_class=pubIpServer, handler_class=pubIpHandler):
server_address = ('', 8021)
print('Run the server at {}'.format(server_address))
http = server_class(server_address, handler_class)
http.serve_forever()
if __name__ == '__main__':
run()