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
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)
-
use a socket connection ?? realpython.com/python-socketsProfessor Abronsius– Professor Abronsius2019年02月09日 13:35:34 +00:00Commented Feb 9, 2019 at 13:35
2 Answers 2
you can send data with 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))
and get data with php like this
$req = file_get_contents('php://input');
$arr = json_decode($req);
6 Comments
I can't comment but I have a suggestion that would possibly achieve this. If you put your Python functionality within an API to return the value, then PHP can make curl requests to get that value. If you do this, I'd suggest running your Python code to store the result in a database and the endpoint returns that value.