"""
python中执行cmd查看Windows内存使用情况
"""

import os
import requests, json


# 查看window内存使用情况
def select_memory():
    # 结果列表
    memory_list = []
    # 输入 cmd 命令
    cmd = "systeminfo"
    # 执行 cmd 命令
    res = os.popen(cmd)
    output_cmd_res = res.read()
    goal = output_cmd_res.split('\n')
    for memory in goal:
        if "物理内存" in memory:
            # 处理此类型数据 (物理内存总量:     8,078 MB)
            # 提取出数据
            size_number = memory.split(':')[1].strip().split(' ')[0]
            # 将数据添加到列表中
            memory_list.append(size_number.replace(',', ''))

    return memory_list


# 向服务器发送post请求
def send_msg():
    url = 'http://httpbin.org/post'
    data = {"physical_memory_sum":select_memory()[0], "physical_memory_used":select_memory()[1]}
    response = requests.post(url, json.dumps(data))
    return response


print(send_msg().content.decode('utf-8'))


select_memory()