0

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)
1

2 Answers 2

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);


Sign up to request clarification or add additional context in comments.

6 Comments

urllib2 is not a library that comes with python because when I try to install it with <br> python -m pip install urllib2. it displays an error [cant find a version that satisfies requirement]
is a lib that you can add it!
can you explain the code? like how do you read if the charger is connected from the php-server side?
in php code every request will get and save in $req variable and if data was json will convert to a class that can be used by php
ok, then how would you display if the charger is connected or not in php side?
|
0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.