socket.gaierror: [Errno 11004] getaddrinfo failed Python - 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

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

Unable to Connect to database in phpmyadmin through Python

I'm trying to connect to database of https://www.freesqldatabase.com but it's not connecting through given code.
But as I connect to my database through phpmyadmin it connects.
import mysql.connector
cnx = mysql.connector.connect(user='user', password='pass',
host='host',
database='database',
port='3306')
try:
cursor = cnx.cursor()
cursor.execute("""
select * FROM USERS
""")
result = cursor.fetchall()
print(result)
finally:
cnx.close()
Everytime I run this code I get these errors.
Traceback (most recent call last):
File "C:\Users\kanwa\AppData\Local\Programs\Python\Python38-32\lib\site-packages\mysql\connector\network.py", line 482, in open_connection
addrinfos = socket.getaddrinfo(self.server_host,
File "C:\Users\kanwa\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:
Traceback (most recent call last):
File "as.py", line 2, in <module>
cnx = mysql.connector.connect(user='user', password='pass',
File "C:\Users\kanwa\AppData\Local\Programs\Python\Python38-32\lib\site-packages\mysql\connector\__init__.py", line 179, in connect
return MySQLConnection(*args, **kwargs)
File "C:\Users\kanwa\AppData\Local\Programs\Python\Python38-32\lib\site-packages\mysql\connector\connection.py", line 95, in __init__
self.connect(**kwargs)
File "C:\Users\kanwa\AppData\Local\Programs\Python\Python38-32\lib\site-packages\mysql\connector\abstracts.py", line 716, in connect
self._open_connection()
File "C:\Users\kanwa\AppData\Local\Programs\Python\Python38-32\lib\site-packages\mysql\connector\connection.py", line 206, in _open_connection
self._socket.open_connection()
File "C:\Users\kanwa\AppData\Local\Programs\Python\Python38-32\lib\site-packages\mysql\connector\network.py", line 500, in open_connection
raise errors.InterfaceError(
mysql.connector.errors.InterfaceError: 2003: Can't connect to MySQL server on 'host:3306' (11001 getaddrinfo failed)

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).

My program closes on name resolution error

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

Categories