-
Notifications
You must be signed in to change notification settings - Fork 0
/
ip_synchronizer_client.py
69 lines (63 loc) · 2.33 KB
/
ip_synchronizer_client.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
#!/usr/bin/python3
# -*- coding: utf-8-
"""
Simple code to set a client receiving IP changes from the specified server
"""
import base64
import hashlib
import logging
import os
import pathlib
import socket
import sys
import urllib.request
from logging.handlers import RotatingFileHandler
HOST: str = 'host_or_ip_of_your_server'
PORT: int = 65432
USERNAME: str = 'entry_to_put_in_the_hosts_file_on_server_side'
PASSWORD: str = hashlib.md5(b'secret').hexdigest()
IP: str = ''
with urllib.request.urlopen('https://4.ident.me') as r:
IP = r.read().decode('utf8')
def create_rotating_log(path: str, level: str) -> logging.Logger:
"""
Create the logger with file rotation.
:param path: the path of the main log file
:param level: the log level as defined in logging module
:return: the logger
"""
result: logging.Logger = logging.getLogger("ip_synchronizer_client")
path_obj: pathlib.Path = pathlib.Path(path)
if not os.path.exists(path_obj.parent.absolute()):
os.makedirs(path_obj.parent.absolute())
if os.path.exists(path):
with open(path, 'w', encoding='utf-8') as f:
f.close()
else:
path_obj.touch()
# noinspection Spellchecker
formatter: logging.Formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
console_handler: logging.Handler = logging.StreamHandler()
console_handler.setLevel(level)
console_handler.setFormatter(formatter)
result.addHandler(console_handler)
file_handler: logging.Handler = RotatingFileHandler(path, maxBytes=1024 * 1024 * 5, backupCount=5)
# noinspection PyUnresolvedReferences
file_handler.setLevel(level)
file_handler.setFormatter(formatter)
result.addHandler(file_handler)
# noinspection PyUnresolvedReferences
result.setLevel(level)
return result
logger = create_rotating_log('/tmp/ip_synchronizer_client.log', 'INFO')
logger.info('IP synchronizer client IP: %s', IP)
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
s.connect((HOST, PORT))
s.settimeout(5)
command: str = USERNAME + '@' + PASSWORD + '|' + IP
logger.info('Sending command: %s', command)
s.sendall(base64.b64encode(command.encode('ascii')))
data = s.recv(1024)
logger.info( "Received: %s", data.decode('ascii'))
logger.info('IP synchronizer client stopped')
sys.exit(0)