Connection close after 300 seconds / wait_timeout mysql

I have the following problem:

When I open a connection to the database (with MySQLdb), it closes after 300 seconds if no operation is performed on the database. I have checked the global variable wait_timeout and it is with value 28800 so I do not understand why it closes after 300 seconds. Could you help me?

Thank you very much.

You do not have the ability to change the 300 second timeout. In general, your code should be robust against losing the connection to the database, because there are many reasons why you might lose the connection, not just because of the 300 second timeout.