zhaoqingang
2025-04-10 9ea07e00fc8b92e9b75849859c1ecce77c7096d1
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
import pytz
import platform
import subprocess
 
from datetime import datetime
 
 
def current_time():
    tz = pytz.timezone('Asia/Shanghai')
    return datetime.now(tz)
 
 
def get_machine_id():
    """获取机器的唯一标识"""
    if platform.system() == "Windows":
        # Windows 系统
        command = "wmic csproduct get UUID"
        process = subprocess.Popen(command.split(), stdout=subprocess.PIPE)
        output, _ = process.communicate()
        machine_id = output.decode().strip().split("\n")[1].strip()
    elif platform.system() == "Darwin":
        # macOS 系统
        command = "system_profiler SPHardwareDataType | grep 'Hardware UUID' | awk '{print $4}'"
        process = subprocess.Popen(command, shell=True, stdout=subprocess.PIPE)
        output, _ = process.communicate()
        machine_id = output.decode().strip()
    else:
        # Linux 系统
        machine_id = open("/etc/machine-id").read().strip()
    # print(machine_id)
    return machine_id