作者 lemon

新增转移终端优化方案4

... ... @@ -39,8 +39,8 @@ def message_receive(message):
for cmd in commands:
monitor.execute(cmd)
elif type_ == 'initialize':
commands = json.loads(data_)
monitor.init_config = commands
monitor.init_config = json.loads(data_)
except:
import traceback
print(traceback.format_exc())
... ...
... ... @@ -18,7 +18,7 @@ from lib import control_server
from lib.common import TaskStatus
from lib.facebook import FacebookClient
from lib.sqlhelper import UserList, Status, Config
from utils import parameter
from utils import parameter, netutil, system_info
log = logging.getLogger(__name__)
tostr = lambda x: (x)
... ... @@ -34,6 +34,8 @@ class Monitor(callback.CallBack):
self._listenlist = dict()
self._imei = Config.get('imei', lambda: uuid.uuid1().hex)
self._name = Config.get('name', control_server.get_init_name)
self._host = None
self._version = '1.0.0'
self.executor = ThreadPoolExecutor(50, 'task_thread')
self.init_config = {}
... ... @@ -97,12 +99,26 @@ class Monitor(callback.CallBack):
if not email: return None
return self._listenlist.get(email, None)
@property
def size(self):
return len(self._listenlist)
def members(self):
return list(self._listenlist.keys())
def _info_(self):
data = {
"name": self._name,
"num": self.size,
"version": self._version,
"server": self._socket.ws_url,
}
data.update(system_info.get())
if not self._host:
self._host = netutil.getip()
data['ip'] = self._host
print(data)
def _heartbeat(self):
if self.init_config.get("disableSync"):
return None
... ... @@ -125,6 +141,7 @@ class Monitor(callback.CallBack):
}),
"type": "initialize",
}
self._info_()
self._socket.send(payload)
self._socket.payload_data = self._heartbeat # 替换心跳数据获取方式
... ...
... ... @@ -21,3 +21,5 @@ def get_ws_address(name, source_ws):
except:
new_ws = source_ws
return new_ws, not new_ws == source_ws
... ...
... ... @@ -5,4 +5,5 @@ tornado==4.5.3
pyyaml==5.3
munch==2.5.0
furl==2.1.0
sqlalchemy==1.3.12
\ No newline at end of file
sqlalchemy==1.3.12
psutil==5.6.7
\ No newline at end of file
... ...
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time : 2020-02-16 11:41
# @Author : Lemon
# @File : netutil.py
# @Software: PyCharm
import requests
def _getip_text(url):
try:
r = requests.get(url, headers={"User-Agent": 'curl/7.54.0'}, timeout=1)
if r.status_code == 200:
return {"ip": r.text.strip()}
except:
pass
return {}
def getip(source=-1):
ip = None
hosts = [
'http://icanhazip.com/',
'http://ip.sb/',
'http://ip.3322.net',
'http://myip.dnsomatic.com/',
'http://whatismyip.akamai.com/',
]
if source == -1:
try:
r = requests.head('https://www.163.com', timeout=1)
ip = r.headers.get('cdn-user-ip', None)
except:
pass
if ip:
return ip
else:
return getip(source + 1)
else:
if source == len(hosts):
return None
ip = _getip_text(hosts[source]).get('ip', None)
if ip:
return ip
else:
return getip(source + 1)
if __name__ == '__main__':
ip = lambda: getip()
for _ in range(10):
if callable(ip):
ip = ip()
print(ip)
... ...
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time : 2020-02-16 11:24
# @Author : Lemon
# @File : running_info.py
# @Software: PyCharm
import psutil
import os, datetime
def _get_delta(timestamp):
t = str(datetime.datetime.now() - datetime.datetime.fromtimestamp(timestamp))
try:
t = t[:-7]
except:
pass
return t
def get():
data = psutil.virtual_memory()
memory = "%d" % (int(round(data.percent))) + "%"
cpu = "%0.2f" % psutil.cpu_percent(1) + "%"
p = psutil.Process(os.getpid())
process_time = _get_delta(p.create_time())
boot_time = _get_delta(psutil.boot_time())
return {
'memory': memory,
'cpu': cpu,
'process_time': process_time,
'boot_time': boot_time
}
... ...