Your code looks fine, I literally just did this, and replaced password with the API key, ensured the user name is my Jenkins User ID
and it worked.
One thing that did stop me before replacing my password with the API key was that the URL needed to be https
, perhaps that's why the connection was refused per your traceback?
server = jenkins.Jenkins('https://localhost:8080', username='jenkinsuserid', password='jenkinsapiktoken')
user = server.get_whoami()
version = server.get_version()
print('Hello %s from Jenkins %s' % (user['fullName'], version))