Member 12255411 Ответов: 0

Как отправить и прочитать вывод Python на PHP-сервер


I was working on this simple python script that constantly checks if the charger is connected to the computer and creates a peep sound when the charger is removed from the computer (Sort of anti-theft) and enables system volume if it is disabled with nircmd command.

I was looking for a way of sending this information to PHP server and reading it from there online. Would anyone be able to tell me how to send the output from the client-side to the server-side? Thank you so much in advance and here is the code: 

import os
import ctypes
import time
from ctypes import wintypes
import winsound
import socket
import json
import urllib3
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)
data = //any data

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

What I have tried:

I have tried to send the output to the php server with the view lines of code at the end. I am looking for a way to read and display this output on a php webpape

Richard MacCutchan

Вам нужно будет прочитать ответ, проанализировать содержимое и где-то отобразить результаты.

Member 12255411

Я уже использую приведенный ниже код для отправки информации на php сервер:
импорт в формате JSON
импорт urllib2

data = //любые данные

req = urllib2.Request('http://your адрес php-сервера')
req.add_header('Content-Type', 'application/json')
ответ = urllib2.urlоткрыть(Треб, в формате JSON.дампы(данные))

Знаете ли вы, как читать это на стороне php

0 Ответов