This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: fix smtplib when local host isn't resolvable in dns
Type: Stage:
Components: Library (Lib) Versions:
process
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: Nosy List: arekm, georg.brandl, loewis
Priority: normal Keywords: patch

Created on 2005-08-12 20:18 by arekm, last changed 2022-04-11 14:56 by admin. This issue is now closed.

Files
File name Uploaded Description Edit
python-smtplib.patch arekm, 2005-08-12 20:18 smtp doesn't need for the system to be resolvable
Messages (6)
msg48654 - (view) Author: Arkadiusz Miśkiewicz (arekm) Date: 2005-08-12 20:18
Suppose that hostname gives hostX. hostX doesn't exists in dns nor in /
etc/hosts (it's unresolvable).

Now when trying to connect to smtp service:
>>> import smtplib
>>> smtplib.SMTP('akcyza.pld-linux.org')
Traceback (most recent call last):
  File "<stdin>", line 1, in ?
  File "/usr/share/python2.4/smtplib.py", line 255, in __init__
socket.gaierror: (-2, 'Name or service not known')

The problem is gethostbyname() here:
                 # We can't find an fqdn hostname, so use a domain literal
                addr = socket.gethostbyname(socket.gethostname())
                self.local_hostname = '[%s]' % addr

It's much easier and better to just use getsockname() for local socket. 
Attached patch fixes this problem and doesn't require for the local 
system to be resolvable in dns/hosts.
msg48655 - (view) Author: Martin v. Löwis (loewis) * (Python committer) Date: 2005-08-24 15:01
Logged In: YES 
user_id=21627

Why would getsockname return '0.0.0.0'?
msg48656 - (view) Author: Arkadiusz Miśkiewicz (arekm) Date: 2005-08-24 17:00
Logged In: YES 
user_id=139606

On unconnected socket it returns 0.0.0.0 afaik which doesn't happen 
here. Please drop that part.
msg48657 - (view) Author: Arkadiusz Miśkiewicz (arekm) Date: 2006-03-31 18:40
Logged In: YES 
user_id=139606

Patch is wrong btw. Doesn't allow to create pure object 
without connecting.
msg48658 - (view) Author: Arkadiusz Miśkiewicz (arekm) Date: 2006-03-31 18:57
Logged In: YES 
user_id=139606

Maybe just always fallback to safe value?

--- Lib/smtplib.py.org  2006-03-31 20:57:05.000000000 +0200
+++ Lib/smtplib.py      2006-03-31 20:57:28.000000000 +0200
@@ -255,7 +255,11 @@
                 self.local_hostname = fqdn
             else:
                 # We can't find an fqdn hostname, so use a 
domain literal
-                addr = socket.gethostbyname(socket.
gethostname())
+                addr = '127.0.0.1'
+                try:
+                    addr = socket.gethostbyname(socket.
gethostname())
+                except socket.gaierror:
+                    pass
                 self.local_hostname = '[%s]' % addr

     def set_debuglevel(self, debuglevel):


msg48659 - (view) Author: Georg Brandl (georg.brandl) * (Python committer) Date: 2006-03-31 19:34
Logged In: YES 
user_id=849994

Committed safe patch in rev. 43499, 43500.
History
Date User Action Args
2022-04-11 14:56:12adminsetgithub: 42276
2005-08-12 20:18:58arekmcreate