使用php服务器读取python输出

时间:2019-02-09 13:21:24

标签: php python

我正在研究这个简单的python脚本,该脚本会不断检查充电器是否已连接到计算机,并在从计算机上卸下充电器时(防盗排序)发出窥视声,并在禁用时启用系统音量使用nircmd命令。

我一直在寻找一种将信息发送到PHP服务器并从那里在线读取信息的方法。谁能告诉我如何将输出从客户端发送到服务器端?预先非常感谢,这里是代码

import os
import ctypes
import time
from ctypes import wintypes
import winsound
import socket
class SYSTEM_POWER_STATUS(ctypes.Structure):
    _fields_ = [
        ('ACLineStatus', wintypes.BYTE),
        ('BatteryFlag', wintypes.BYTE),
        ('BatteryLifePercent', wintypes.BYTE),
        ('BatteryLifeTime', wintypes.DWORD),
        ('BatteryFullLifeTime', wintypes.DWORD),
    ]
print(socket.gethostname())

SYSTEM_POWER_STATUS_P = ctypes.POINTER(SYSTEM_POWER_STATUS)

GetSystemPowerStatus = ctypes.windll.kernel32.GetSystemPowerStatus
GetSystemPowerStatus.argtypes = [SYSTEM_POWER_STATUS_P]
GetSystemPowerStatus.restype = wintypes.BOOL
status = SYSTEM_POWER_STATUS()
while True: 
        if not GetSystemPowerStatus(ctypes.pointer(status)):
            raise ctypes.WinError()
        if(status.ACLineStatus==1):
            print('Charger Connected: ', "YES")
        elif(status.ACLineStatus==0):
            print('Charger Connected: ', "NO")
            winsound.Beep(2000,1000)
        if(status.BatteryFlag==-128):
            print('Battery removed: ', "Yes")
            winsound.Beep(2000,1000)
        time.sleep(1)
        if(status.ACLineStatus==0):
                cmd = 'nircmd.exe mutesysvolume 0 && nircmd.exe setsysvolume 65535'
                os.system(cmd)

2 个答案:

答案 0 :(得分:2)

您可以使用python发送数据

import json
import urllib2

data = //any data

req = urllib2.Request('http://your php server address')
req.add_header('Content-Type', 'application/json')
response = urllib2.urlopen(req, json.dumps(data))

并使用php这样获取数据

$req = file_get_contents('php://input');
$arr = json_decode($req);


答案 1 :(得分:0)

我无法发表评论,但我有一个建议可能会实现这一目标。如果将Python功能放在API中以返回值,则PHP可以发出curl请求以获取该值。如果这样做,建议您运行Python代码以将结果存储在数据库中,然后端点返回该值。