One thing I would recommend is trying the code as a simple python script on your local machine.
This way you can narrow down the bug to its most simple parts.
If you get to a point when you can connect to the server on your local machine with the code snippet then you can see what changes if you run the code in anvil’s server module.
You also may want to check you whitelisted the correct IP address(es). See this post