简体   繁体   English

PHP服务器处理Python请求发布

[英]PHP Server Processing Python Requests Post

Trying to build a client/server app with Python as the client and PHP as the server. 尝试使用Python作为客户端和PHP作为服务器来构建客户端/服务器应用程序。

Testing the server-side receiving now to be able to process data but I'm coming up empty. 现在测试服务器端接收到的数据,以便能够处理数据,但是我空了。

Client: 客户:

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()

Server: 服务器:

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

I'm receiving a 200 okay response but with a response that $_POST is completely empty. 我收到200好的响应,但响应中$ _POST完全为空。

I've also tried 我也尝试过

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

All to no avail. 一切都无济于事。 Am I missing something to be able to process the POST request on a PHP server? 我是否缺少能够在PHP服务器上处理POST请求的内容?

EDIT: An example of the machine variable with actual data is 编辑:带有实际数据的机器变量的示例是

{
    "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
}

It is a longshot with the given info but I think you have an index file priority issue. 与给定的信息比较远,但是我认为您有索引文件优先级问题。

Could you add index.php to your endpoint, http://monitor.localhost.local/api/index.php 您能否将index.php添加到您的端点http://monitor.localhost.local/api/index.php

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM