0

Hello gears I'm trying to run a python script from a site, following your recommendation I use ajax but it brings me back the python code and does not execute the code.

Here is the code on the browser side

<script>

 document.getElementById("button").onclick = function () {
    $.ajax({
        type: 'POST',
        url: "projet.py",
        data: {param: ""}, //passing some input here
        success: function(response){
           console.log(output);
        }
     })
  };

</script>

  <div class="button" id="button">
    Start
</div>
  #!/usr/bin/python3
import pymysql.cursors
import time
import serial

# Connect to the database
connection = pymysql.connect(host='localhost',
                             user='root',
                             password='******',
                             database='iot',
                             charset='utf8mb4',
                             cursorclass=pymysql.cursors.DictCursor)
time.sleep(0.5)

while 1 :
    ser = serial.Serial('/dev/ttyACM0', 9600, timeout=1)
    ser.flush()
    while True:
        if ser.in_waiting > 0:
            line = ser.readline().decode('utf-8').rstrip()
            print("Valeur recu :{}".format(line))
            break
    time.sleep(0.5)
    with connection:
        with connection.cursor() as cursor:
            # Create a new record
            sql = "INSERT INTO `tension` (`valeur`) VALUES (%s)"
            cursor.execute(sql, (line))

        # connection is not autocommit by default. So you must commit to save
        # your changes.
        connection.commit()
        time.sleep(0.5)
koh kone
  • 1
  • 2

0 Answers0