1

I get this error:

  File "/usr/lib/python2.7/dist-packages/paramiko/client.py", line 286, in connect
    for (family, socktype, proto, canonname, sockaddr) in socket.getaddrinfo(hostname, port, socket.AF_UNSPEC, socket.SOCK_STREAM):
gaierror: [Errno -2] Name or service not known

When I enter wrong hostname. I want to put an exception, giving message that wrong hostname was entered, but Python does not recognize that error and I get global name gaierror not defined.

I was trying like this:

try:
    ssh.connect(rec.host, username=rec.user, password=rec.password)
except gaierror:
    print 'blablabla'

Then it gives this error:

    except gaierror:
NameError: global name 'gaierror' is not defined

Do I need to define that error myself somehow or I need to call something from paramiko, so python would understand that exception?

2
  • How do you know a host is wrong? Commented Oct 15, 2014 at 11:49
  • Because I entered not existing host (on purpose ofc)?.. And when it does not find such host, it throws that gaierror Commented Oct 15, 2014 at 11:50

1 Answer 1

2

Import the gaierror exception from the socket module. Docs

import socket

try:
    # Your code.
except socket.gaierror:
    # Handle exception.
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.