Py学习  »  Python

使用php服务器读取python输出

Monolica • 4 年前 • 242 次点击  

我正在编写一个简单的python脚本,该脚本不断检查充电器是否连接到计算机,并在充电器从计算机上卸下时创建一个peep声音(有点防盗),如果使用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)
Python社区是高质量的Python/Django开发社区
本文地址:http://www.python88.com/topic/50966
 
242 次点击  
文章 [ 2 ]  |  最新文章 4 年前
Dillan Wilding
Reply   •   1 楼
Dillan Wilding    5 年前

我不能评论,但我有一个可能实现这一点的建议。如果将Python功能放在API中以返回值,那么PHP可以发出curl请求来获取该值。如果这样做,我建议运行Python代码将结果存储在数据库中,并且端点返回该值。

Ali Ghalambaz
Reply   •   2 楼
Ali Ghalambaz    5 年前

可以用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);