-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtask-post.py
More file actions
84 lines (72 loc) · 2.56 KB
/
task-post.py
File metadata and controls
84 lines (72 loc) · 2.56 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
#print(f'Hello, {req.encoding}!{req.status_code} How are you?')
#SELECT * FROM lampdb2.Telemetry ORDER BY id DESC;
import requests
import mysql.connector
from mysql.connector import Error
import time
import configparser
index='testValue'
#Read from .ini file
config = configparser.ConfigParser()
config.read('credentials.ini')
host = config['mysql']['host']
user = config['mysql']['user']
password = config['mysql']['password']
database = config['mysql']['database']
# Define new data to create
new_data = {
"musicbox_id": 284,
"telemetry": index
}
# The API endpoint to communicate with
url_post = "http://test.ru/api/v1/telemetry.php"
get_response = requests.get(url_post)
if get_response.status_code == 200:
print('POST success!')
elif get_response.status_code != 200:
print('Error!')
print(get_response.status_code)
# A POST request to the API
post_response = requests.post(url_post, json=new_data)
# Print the response
post_response_json = post_response.json()
print(post_response_json)
####################################################
#Working with DB
####################################################
print('Whait for process post request!')
time.sleep(10)
print('Go again!')
try:
connection = mysql.connector.connect(host=host,
database=database,
user=user,
password=password)
if connection.is_connected():
db_Info = connection.get_server_info()
print("Connected to MySQL Server version ", db_Info)
cursor = connection.cursor()
#cursor.execute("select database();")
#record = cursor.fetchone()
#print("You're connected to database: ", record)
#Select
cursor.execute("SELECT telemetry FROM lampdb2.Telemetry WHERE musicbox_id = '284' AND telemetry = '\"%s\"' ORDER BY id DESC LIMIT 1;" % index)
myresult = cursor.fetchall()
y=0
for x in myresult:
y+=1
print(f"y={y} value: {x}")
if y == 1:
print("DB filled. Post query work.")
else:
print("The data has not been recorded in the database. Post query did not work. y=%s" % index)
#Delite
cursor.execute("DELETE FROM lampdb2.Telemetry WHERE musicbox_id = '284' AND telemetry = '\"%s\"';" % index)
connection.commit()
except Error as e:
print("Error while connecting to MySQL", e)
finally:
if connection.is_connected():
cursor.close()
connection.close()
print("MySQL connection is closed")