[英]PHP Server Processing Python Requests Post
嘗試使用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好的響應,但響應中$ _POST完全為空。
我也嘗試過
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
}
與給定的信息比較遠,但是我認為您有索引文件優先級問題。
您能否將index.php
添加到您的端點http://monitor.localhost.local/api/index.php
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.