处理Python请求的PHP服务器Pos

2024-04-25 21:58:22 发布

您现在位置:Python中文网/ 问答频道 /正文

尝试用Python作为客户机,PHP作为服务器来构建客户机/服务器应用程序。你知道吗

现在正在测试服务器端接收,以便能够处理数据,但我的回答是空的。你知道吗

客户:

import requests, json
def main():
    ## Functions to get system info working fine

    machine = {
        "hostname" : hostname,
        "system" : system,
        "cpu_count" : cpu_count,
        "cpu_usage" : cpu_usage,
        "memory_total" : memory_total,
        "memory_used" : memory_used,
        "memory_used_percent" : memory_used_percent,
        "drives" : disks,
        "network_up" : network_stats["traffic_out"],
        "network_down" : network_stats["traffic_in"]
    }
    # print(machine) displays expected output
    post_data(machine)

def post_data(machine):
    try:
        endpoint = "http://monitor.localhost.local/api/"
        r = requests.post(url = endpoint, json = machine)
        print("POST:",r)
        print(r.content)
    except requests.exceptions.RequestException as e:
        print("\nPOST Error:\n",e)

main()

服务器:

<?php
    header('Access-Control-Allow-Origin: *');
    var_dump($_POST);
?>

我收到了一个200元的回复,但回复是$邮是完全空的。你知道吗

我也试过了

data = json.dumps(machine)
post_data(data)
...
r = requests.post(url = endpoint, data= machine)

一切都无济于事。在PHP服务器上处理POST请求时,我是否遗漏了一些东西?你知道吗

编辑: 具有实际数据的机器变量示例如下

{
    "hostname": "WORK-LAPTOP1",
    "system": {
        "name": "Windows",
        "version": "10"
    },
    "cpu_count": 4,
    "cpu_usage": 17.9,
    "memory_total": 8440942592,
    "memory_used": 6244225024,
    "memory_used_percent": 74.0,
    "drives": [
        {
            "name": "C:\\",
            "mount_point": "C:\\",
            "type": "NTFS",
            "total_size": 536224985088,
            "used_size": 167306108928,
            "percent_used": 31.2
        },
        {
            "name": "D:\\",
            "mount_point": "D:\\",
            "type": "NTFS",
            "total_size": 463332921344,
            "used_size": 49498419200,
            "percent_used": 10.7
        }
    ],
    "network_up": 54,
    "network_down": 4150
}

Tags: 服务器jsondatasizenetworkcpumachinepost