Adding exceptions for not recognized error? - python

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?

Import the gaierror exception from the socket module. Docs
import socket
try:
# Your code.
except socket.gaierror:
# Handle exception.

Related

Error :- socket.gaierror: [Errno 11001] getaddrinfo failed in get request

import socket
mysocket=socket.socket(socket.AF_INET,socket.SOCK_STREAM)
mysocket.connect(("http://www.py4inf.com",80))
c="http://www.py4inf.com/code/romeo.txt HTTP/1.0\r\n\r\n".encode()
mysocket.send(c)
while True:
data=mysocket.recv(512)
if len(data) <1:
break
else:
print(data.decode())
mysocket.close()
Error:-
Traceback (most recent call last):
File "D:\Python_practice_file\main.py", line 3, in <module>
mysocket.connect(("http://www.py4inf.com",80))
socket.gaierror: [Errno 11001] getaddrinfo failed
I am getting this error while I am learning web socket programming in python but it seems like I am getting an error if anyone can solve this problem I'll be very grateful. I saw some answers but they are not related because i am not using proxy,
Remove the http:// part.
This indicates protocol, which is not part of the FQDN getaddrinfo() tries to resolve.
Also change
c="http://www.py4inf.com/code/romeo.txt HTTP/1.0\r\n\r\n"
to
c="GET /code/romeo.txt HTTP/1.0\r\n\r\n"
But I suggest using urllib or requests libraries if you want to do HTTP requests.

How to use Boto3 S3 connection with proxy?

I want to write the Python script to download from S3 bucket using Boto3. My code is working fine when not using proxy, but I have to run this under proxy. I have set proxy in the Boto3.client(config=) and I get the following error.
Proxy set as:
s3 = boto3.client('s3',
aws_access_key_id = usrkey,
aws_secret_access_key = sctkey,
region_name = 'eu-west-2',
config=Config(proxies={'http': '<proxy-server>:<port>'})
)
still end up with the error
Traceback (most recent call last):
File "C:\Users\Hello\AppData\Local\Programs\Python\Python38-32\lib\site-packages\urllib3\connection.py", line 159, in _new_conn
conn = connection.create_connection(
File "C:\Users\Hello\AppData\Local\Programs\Python\Python38-32\lib\site-packages\urllib3\util\connection.py", line 61, in create_connection
for res in socket.getaddrinfo(host, port, family, socket.SOCK_STREAM):
File "C:\Users\Hello\AppData\Local\Programs\Python\Python38-32\lib\socket.py", line 918, in getaddrinfo
for res in _socket.getaddrinfo(host, port, family, type, proto, flags):
socket.gaierror: [Errno 11001] getaddrinfo failed
During handling of the above exception, another exception occurred:
and at the end:
botocore.exceptions.EndpointConnectionError: Could not connect to the endpoint URL:"<URL>".
I can connect using WinSCP to AWS-S3, with proxy, that is working fine. But it pops up a warning message that I have to accept every time, by clicking ok. What is the problem here, I don't understand with Python.
Make sure your <proxy-server> is a host name:
config=Config(proxies={'http': 'example.com:port'}
Not a URL:
config=Config(proxies={'http': 'http://example.com:port'}

Unable to connect to server using Paramiko package

I'm trying to connect to my server using SSH with port 2022 (not 22) in Python. So I wrote the following code that uses Paramiko package:
import sys
import paramiko
try:
client = paramiko.SSHClient()
client.load_system_host_keys()
client.set_missing_host_key_policy(paramiko.WarningPolicy)
client.connect('ccap#10.40.2.222', '2022', '', 'ccap')
finally:
client.close()
But when I'm running it in my IDE (PyCharm) I get the following error:
/usr/local/lib/python3.5/dist-packages/paramiko/ecdsakey.py:164: CryptographyDeprecationWarning: Support for unsafe construction of public numbers from encoded data will be removed in a future version. Please use EllipticCurvePublicKey.from_encoded_point
self.ecdsa_curve.curve_class(), pointinfo
Traceback (most recent call last):
File "/home/mshapirs/PycharmProjects/OnlineTest.py/OnlineTest.py", line 9, in
client.connect('ccap#10.40.2.222', '2022', '', 'ccap')
File "/usr/local/lib/python3.5/dist-packages/paramiko/client.py", line 334, in connect
to_try = list(self._families_and_addresses(hostname, port))
File "/usr/local/lib/python3.5/dist-packages/paramiko/client.py", line 204, in _families_and_addresses
hostname, port, socket.AF_UNSPEC, socket.SOCK_STREAM
File "/usr/lib/python3.5/socket.py", line 733, in getaddrinfo
for res in _socket.getaddrinfo(host, port, family, type, proto, flags):
socket.gaierror: [Errno -2] Name or service not known
You should provide username as a separate parameter, not prepended to the host address.
Look at the docs for .connect. It has username and hostnamelisted separately.

What error/exception does Paramiko throw for failed connects?

If this fails:
ssh = paramiko.SSHClient()
ssh.connect( host, username = USER , pkey = MY_KEY, timeout = 2)
I get a traceback like:
File "<stdin>", line 1, in <module>
File "<stdin>", line 7, in bs_process
File "/usr/lib/python2.7/site-packages/paramiko/client.py", line 282, in connect
for (family, socktype, proto, canonname, sockaddr) in socket.getaddrinfo(hostname, port, socket.AF_UNSPEC, socket.SOCK_STREAM):
socket.gaierror: [Errno -2] Name or service not known
I cant figure what kind/kinds of errors Paramiko throws for bad connect attempts. Which are the exception classes and how can I import them?
You can start by looking at the API documentation, for all classes ending in Exception:
http://docs.paramiko.org/en/1.15/api/client.html#paramiko.client.SSHClient.connect
Then, you should also catch socket.error. I think that will get you pretty much everything. socket.gaierror is a subclass of socket.error, for example.
The accepted answer has a broken link. The documentation for Paramiko now lives at:
http://docs.paramiko.org/en/1.15/api/client.html#paramiko.client.SSHClient.connect
It the "connect" method will raise the following:
BadHostKeyException – if the server’s host key could not be verified
AuthenticationException – if authentication failed
SSHException – if there was any other error connecting or establishing an SSH session
socket.error – if a socket error occurred while connecting
The problem is with the call to ssh.connect().
In this case, is necessarily specified the connection port.
Example:
ssh.connect(server, port=22, username=user, pkey=key)
That work for me.

sending mail error python

i get the following error when trying a script thaat sends mail
import urllib.request
import re
import smtplib
from email.mime.text import MIMEText
from bs4 import BeautifulSoup
page=urllib.request.urlopen("http://www.crummy.com/")
soup=BeautifulSoup(page)
v=soup.findAll('a',href=re.compile('http://www.crummy.com/2012/07/24/0'))
for link in v:
w=link.get('href')
server = smtplib.SMTP( "smtp.gmail.com", 587 )
server.starttls()
server.login( 'xxxxxxxxxxx', 'xxxxxxx' )
server.sendmail( 'xxxxxxxxx', 'xxxxxxxxx', "bonus question is up" )
Traceback (most recent call last): File "C:\Python32\bonus", line 14,
in server = smtplib.SMTP( "smtp.gmail.com", 587 ) File
"C:\Python32\lib\smtplib.py", line 259, in init File
"C:\Python32\lib\smtplib.py", line 319, in connect
self.sock = self._get_socket(host, port, self.timeout) File "C:\Python32 \lib\smtplib.py", line 294, in _get_socket
return socket.create_connection((host, port), timeout) File "C:\Python32\lib\socket.py", line 386, in create_connection
for res in getaddrinfo(host, port, 0, SOCK_STREAM): socket.gaierror: [Errno 11004] getaddrinfo failed plse advice on the
best way to go round it
The getaddrinfo function has this purpose:
The getaddrinfo function provides protocol-independent translation from an ANSI host name to an address.
If it fails it means that it cannot translate your given hostname to it's respective address. It's essentially doing a DNS query.
Your error number returned "11004" by getaddrinfo has this message associated with it:
Valid name, no data record of requested type. The requested name is
valid and was found in the database, but it does not have the correct
associated data being resolved for. The usual example for this is a
host name-to-address translation attempt (using gethostbyname or
WSAAsyncGetHostByName) which uses the DNS (Domain Name Server). An MX
record is returned but no A record—indicating the host itself exists,
but is not directly reachable.
It seems the name you're looking up has no correct data associated with it.
Are you sure your URL's are correct?
Links:
getaddrinfo: http://msdn.microsoft.com/en-us/library/windows/desktop/ms738520(v=vs.85).aspx
WinSock Error Codes: http://msdn.microsoft.com/en-us/library/windows/desktop/ms740668(v=vs.85).aspx

Categories

Resources