Sergiu Fratila
Sergiu Fratila

Reputation: 51

Python socket.gaierror: [Errno 11001] getaddrinfo failed

I'm trying to make a mail sender in Python, the script works on my personal laptop, but when I run it on my work laptop, I think the proxy gets in the way of getting a connection to the Gmail SMTP server

The error is as follows:

File "D:\ocm-hours-report-automation\mail-manager\src\python\mail-sender.py", line 44, in <module>
    session = smtplib.SMTP('smtp.gmail.com', 587) #use gmail with port
  File "C:\Users\myuser\AppData\Local\Programs\Python\Python39\lib\smtplib.py", line 255, in __init__
    (code, msg) = self.connect(host, port)
  File "C:\Users\myuser\AppData\Local\Programs\Python\Python39\lib\smtplib.py", line 341, in connect
    self.sock = self._get_socket(host, port, self.timeout)
  File "C:\Users\myuser\AppData\Local\Programs\Python\Python39\lib\smtplib.py", line 312, in _get_socket
    return socket.create_connection((host, port), timeout,
  File "C:\Users\myuser\AppData\Local\Programs\Python\Python39\lib\socket.py", line 822, in create_connection
    for res in getaddrinfo(host, port, 0, SOCK_STREAM):
  File "C:\Users\myuser\AppData\Local\Programs\Python\Python39\lib\socket.py", line 953, in getaddrinfo
    for res in _socket.getaddrinfo(host, port, family, type, proto, flags):
socket.gaierror: [Errno 11001] getaddrinfo failed

The code is:

#The mail addresses and password
sender_address = '[email protected]'
sender_pass = 'password'
receiver_address = '[email protected]'
#Setup the MIME
message = MIMEMultipart()
message['From'] = sender_address
message['To'] = receiver_address
message['Subject'] = 'A test mail sent by Python. It has an attachment.'   #The subject line
#The body and the attachments for the mail
message.attach(MIMEText(mail_content, 'plain'))
#Create SMTP session for sending the mail
session = smtplib.SMTP('smtp.gmail.com', 587) #use gmail with port
session.starttls() #enable security
session.login(sender_address, sender_pass) #login with mail_id and password
text = message.as_string()
session.sendmail(sender_address, receiver_address, text)
session.quit()
print('Mail Sent')

Does someone have any idea what I can actually do? I tried forcing a socks.setdefaultproxy but it said the socks import is not available.

Upvotes: 4

Views: 32525

Answers (4)

Gautam Pramod
Gautam Pramod

Reputation: 1

I had the same problem and I think Windows cannot recognize 0:8000. Here 0 is the local server i.e. localhost. As a network interface. So in order to get rid of this problem either we run python manage.py runserver 0.0.0.0:8000 or 127.0.0.1:8000 or just python manage.py runserver.

Upvotes: 0

Kolja
Kolja

Reputation: 51

Rookie mistake, but in my case, the problem was the starting-parameters of the app. I ran

python manage.py  runserver myappname:8001 --insecure // wrong

I FIXED the error by using the current "localhost" address on startup:

python manage.py  runserver 0.0.0.0:8001 --insecure // working

Upvotes: 1

usman Abbasi
usman Abbasi

Reputation: 107

In my case, I was getting this error because I'd lost my internet connection. I think I should share it here so that someone will not have to waste time

Upvotes: 2

chrisxfire
chrisxfire

Reputation: 625

Your suspicion is correct. socket.gaierror: [Errno 11001] getaddrinfo failed indicates the application cannot resolve the IP address of the host. There's various ways to get around this depending on the operating system of the machine running the code such as manually mapping in /etc/hosts. But, even if the IP address is resolved, that doesn't mean the proxy will allow a connection to it.

Upvotes: 2

Related Questions