Created
August 4, 2023 19:44
-
-
Save chintanop/7f063fceef92737f2f3e3a7ba2355608 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import MySQLdb | |
import time | |
def get_connection(): | |
return MySQLdb.connect(host='localhost', | |
user='your_username', | |
passwd='your_password', | |
db='your_database') | |
def query(conn, sql): | |
try: | |
cursor = conn.cursor() | |
cursor.execute(sql) | |
return cursor.fetchall() | |
except MySQLdb.OperationalError as e: | |
print(f"Error: {e}") | |
return None | |
def main(): | |
conn = get_connection() | |
start_time = time.time() # Record the start time | |
while True: | |
result = query(conn, 'SELECT 1') | |
if result is None: # If there was an error, try to reconnect. | |
print("Reconnecting...") | |
conn = get_connection() | |
else: | |
print(result) | |
elapsed_time = time.time() - start_time # Calculate elapsed time | |
print(f"Elapsed time: {elapsed_time} seconds") | |
time.sleep(5) # Wait for a bit to avoid flooding the server with requests. | |
if __name__ == '__main__': | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment