My program closes on name resolution error - python

My program closes on name resolution error. This could happen when there is no internet or the other server is down. I want program to raise exception, but not exit. How do I do this. I'm using paho client.
File "/home/pi/.local/lib/python3.5/site-packages/paho/mqtt/client.py",
line 760, in connect
return self.reconnect()
File "/home/pi/.local/lib/python3.5/site-packages/paho/mqtt/client.py",
line 887, in reconnect
sock = socket.create_connection((self._host, self._port),
source_address=(self._bind_address, 0))
File "/usr/lib/python3.5/socket.py", line 694, in create_connection
for res in getaddrinfo(host, port, 0, 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 -3] Temporary failure in name resolution

Related

python3 ntplib socket.gaierror: [Errno -8] Servname not supported for ai_socktype

When using ntplib.NTPClient().request(), it crash. It happened on AidLux on Android, but it can run correctly on macOS.
Traceback (most recent call last):
File "main.py", line 82, in ntplib.NTPClient().request('ntp.aliyun.com').tx_time
File "/usr/local/lib/python3.7/dist-packages/ntplib.py", line 296, in request
addrinfo = socket.getaddrinfo(host, port)[0]
File "/usr/lib/python3.7/socket.py", line 748, in getaddrinfo
for res in _socket.getaddrinfo(host, port, family, type, proto, flags):
socket.gaierror: [Errno -8] Servname not supported for ai_socktype

SQLAlqhemy database operational error 11001

When I try to user the command db.create_all() to build my sqlalchemy database (with flask-sqlalchemy) for my flask application I get an issue, and I can not fix this, this is the error code:
File "c:\users\paulo\desktop\python\projects\paper_note\venv\lib\site-packages\pymysql\connections.py", line 614, in connect
(self.host, self.port), self.connect_timeout, **kwargs
File "C:\Program Files\Python37\lib\socket.py", line 707, in create_connection
for res in getaddrinfo(host, port, 0, SOCK_STREAM):
File "C:\Program Files\Python37\lib\socket.py", line 752, 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:
File "c:\users\paulo\desktop\python\projects\paper_note\venv\lib\site-packages\sqlalchemy\engine\default.py", line 509, in connect
return self.dbapi.connect(*cargs, **cparams)
File "c:\users\paulo\desktop\python\projects\paper_note\venv\lib\site-packages\pymysql\connections.py", line 353, in __init__
self.connect()
File "c:\users\paulo\desktop\python\projects\paper_note\venv\lib\site-packages\pymysql\connections.py", line 664, in connect
raise exc
pymysql.err.OperationalError: (2003, "Can't connect to MySQL server on 'dbserver' ([Errno 11001] getaddrinfo failed)")
I hope this will help other people, to fix this error I verified my database connection URL, and that was incorrect.

SMTP send: "getaddrinfo failed"

import smtplib
def sendEmail(receiver,subject,message):
server = smtplib.SMTP('smtp.gamil.com',587)
server.ehlo()
server.starttls()
server.login("botsinhala#gmail.com",password)
server.sendmail("botsinhala#gmail.com",receiver,message)
print("succesful")
sendEmail("gadjetrathnayake#gmail.com","testing bot","Hi, This is a testing message i'm sending to you.")
above code is generating the following error. My OS is windows 10.
Traceback (most recent call last):
File "D:/MY CHANNEL/python tricks/Email bot 1/tttttttttttttttttttttttt.py", line 17, in <module>
sendEmail("andrewhawkins#gmail.com","testing bot","Hi, This is a testing message i'm sending to you.")
File "D:/MY CHANNEL/python tricks/Email bot 1/tttttttttttttttttttttttt.py", line 5, in sendEmail
server = smtplib.SMTP('smtp.gamil.com',587)
File "C:\Users\Dell Pc\AppData\Local\Programs\Python\Python37-32\lib\smtplib.py", line 251, in __init__
(code, msg) = self.connect(host, port)
File "C:\Users\Dell Pc\AppData\Local\Programs\Python\Python37-32\lib\smtplib.py", line 336, in connect
self.sock = self._get_socket(host, port, self.timeout)
File "C:\Users\Dell Pc\AppData\Local\Programs\Python\Python37-32\lib\smtplib.py", line 307, in _get_socket
self.source_address)
File "C:\Users\Dell Pc\AppData\Local\Programs\Python\Python37-32\lib\socket.py", line 707, in create_connection
for res in getaddrinfo(host, port, 0, SOCK_STREAM):
File "C:\Users\Dell Pc\AppData\Local\Programs\Python\Python37-32\lib\socket.py", line 748, in getaddrinfo
for res in _socket.getaddrinfo(host, port, family, type, proto, flags):
socket.gaierror: [Errno 11001] getaddrinfo failed
How can I fix this error
Replace smtp.gamil.com by smtp.gmail.com.
There are no A/AAAA records for smtp.gamil.com (2021-01-07T13:08+00:00).

socket.gaierror: [Errno 11004] getaddrinfo failed Python

Im currently having a problem with a Python program I made. The program is supposed to connect to google translate using the http.client module. My program works if im just connection to google.com, but it gives me an error when I try to connect to translate.google.com.
import urllib.request,re,http.client
while True:
print('')
question = input(">: ").replace(" ","%20").replace("?",'%3F')
x = "/#auto/es/%s"%(question)
conn = http.client.HTTPSConnection("www.translate.google.com",80)
http.client.HTTPConnection.connect(conn)
conn.request("GET",x)
response = conn.getresponse()
data = str(response.read())
conn.close()
print("Done")
Traceback (most recent call last):
File "P:\CS5\translater.py", line 9, in <module>
http.client.HTTPConnection.connect(conn)
File "C:\Program Files\Python35-32\lib\http\client.py", line 826, in connect
(self.host,self.port), self.timeout, self.source_address)
File "C:\Program Files\Python35-32\lib\socket.py", line 693, in create_connection
for res in getaddrinfo(host, port, 0, SOCK_STREAM):
File "C:\Program Files\Python35-32\lib\socket.py", line 732, in getaddrinfo
for res in _socket.getaddrinfo(host, port, family, type, proto, flags):
socket.gaierror: [Errno 11004] getaddrinfo failed

Python 3.4: socket.gaierror: [Errno -2] on header request

I need to make a simple header request to every URL in a large set, to check if they are still available. Now I made the following code:
from http import client
for i, triple in enumerate(catalouge):
connection = client.HTTPConnection(triple[2].strip('http://'))
connection.request('HEAD', '/')
print(connection.getresponse().status + ' on entry ' + str(i+1))
Now catalouge is a set of all the links with the 3rd element being the URL which needs to be checked. The .strip('http://') part is needed since I will receive this error otherwise:
http.client.InvalidURL: nonnumeric port:
With this code in place I now receive this error:
Traceback (most recent call last):
[...]
connection.request('HEAD', '/')
File "/usr/lib/python3.4/http/client.py", line 1137, in request
self._send_request(method, url, body, headers)
File "/usr/lib/python3.4/http/client.py", line 1182, in _send_request
self.endheaders(body)
File "/usr/lib/python3.4/http/client.py", line 1133, in endheaders
self._send_output(message_body)
File "/usr/lib/python3.4/http/client.py", line 963, in _send_output
self.send(msg)
File "/usr/lib/python3.4/http/client.py", line 898, in send
self.connect()
File "/usr/lib/python3.4/http/client.py", line 871, in connect
self.timeout, self.source_address)
File "/usr/lib/python3.4/socket.py", line 494, in create_connection
for res in getaddrinfo(host, port, 0, SOCK_STREAM):
File "/usr/lib/python3.4/socket.py", line 533, in getaddrinfo
for res in _socket.getaddrinfo(host, port, family, type, proto, flags):
socket.gaierror: [Errno -2] Name or service not known
Did I miss something? Any suggestion would be very appreciated.

Categories