-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest2.py
57 lines (53 loc) · 1.79 KB
/
test2.py
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
import logging
import asyncio
import pymysql
from typing import List, Dict, Tuple
import ping3
from telegram import Update
from telegram.ext import ApplicationBuilder, ContextTypes, CommandHandler
logging.basicConfig(
format='%(asctime)s - %(name)s - %(levelname)s - %(message)s',
level=logging.INFO
)
db_config = {
'host': '192.168.27.185',
'user': 'saideep',
'password': 'Lenskart@123#',
'database': 'shuttle_state',
'port': 3306
}
def populate_initial_data(file_path):
shuttles = []
try:
with open(file_path, 'r') as file:
for line in file:
if line.strip():
parts = line.strip().split(',')
if len(parts) == 2:
name = parts[0].strip()
ip_address = parts[1].strip()
shuttles.append((name, ip_address))
except FileNotFoundError:
logging.error(f"File '{file_path}' not found.")
except Exception as e:
logging.error(f"Error reading file '{file_path}': {str(e)}")
return shuttles
def insert_initial_shuttles(shuttles):
try:
connection = pymysql.connect(**db_config)
cursor = connection.cursor()
for name, ip in shuttles:
query = """INSERT INTO shuttle_status
(shuttle_name, shuttle_ip, shuttle_state, state_timestamp)
VALUES (%s, %s, 'YTC', NOW())"""
cursor.execute(query, (name, ip))
connection.commit()
except pymysql.MySQLError as e:
logging.error(f"Error executing query: {e}")
finally:
if connection:
cursor.close()
connection.close()
if __name__ == "__main__":
shuttles = populate_initial_data('temp_shuttles.txt')
insert_initial_shuttles(shuttles)