From caa658d04739875133f388d9dc470843e3870f87 Mon Sep 17 00:00:00 2001 From: Thomas Wouters Date: Tue, 15 Aug 2000 19:30:36 +0000 Subject: Apply SF patch #101151, by Peter S-K, which fixes smtplib's passing of the 'helo' and 'ehlo' message, and exports the 'make_fqdn' function. This function should be moved to socket.py, if that module ever gets a Python wrapper. --- Doc/lib/libsocket.tex | 2 ++ Lib/smtplib.py | 40 +++++++++++++++++++++++++++------------- 2 files changed, 29 insertions(+), 13 deletions(-) diff --git a/Doc/lib/libsocket.tex b/Doc/lib/libsocket.tex index 6b44e15..6f7ab1e 100644 --- a/Doc/lib/libsocket.tex +++ b/Doc/lib/libsocket.tex @@ -136,6 +136,8 @@ and \var{ipaddrlist} is a list of IP addresses for the same interface on the same host (most likely containing only a single address). To find the fully qualified domain name, check \var{hostname} and the items of \var{aliaslist} for an entry containing at least one period. +An implementation of this algorithm can be found in the module +\module{smtplib} in form of the \function{make_fqdn()} function. \end{funcdesc} \begin{funcdesc}{getprotobyname}{protocolname} diff --git a/Lib/smtplib.py b/Lib/smtplib.py index f898a2f..f00f30b 100755 --- a/Lib/smtplib.py +++ b/Lib/smtplib.py @@ -133,21 +133,29 @@ def quotedata(data): return re.sub(r'(?m)^\.', '..', re.sub(r'(?:\r\n|\n|\r(?!\n))', CRLF, data)) -def _get_fqdn_hostname(name): +def make_fqdn(name = ''): + """Get fully qualified domain name from name. + + An empty argument is interpreted as meaning the local host. + + First the hostname returned by socket.gethostbyaddr() + is checked, then possibly existing aliases. In case + no FQDN is available, hostname is returned. + """ name = string.strip(name) if len(name) == 0: name = socket.gethostname() - try: - hostname, aliases, ipaddrs = socket.gethostbyaddr(name) - except socket.error: - pass + try: + hostname, aliases, ipaddrs = socket.gethostbyaddr(name) + except socket.error: + pass + else: + aliases.insert(0, hostname) + for name in aliases: + if '.' in name: + break else: - aliases.insert(0, hostname) - for name in aliases: - if '.' in name: - break - else: - name = hostname + name = hostname return name @@ -306,7 +314,10 @@ class SMTP: Hostname to send for this command defaults to the FQDN of the local host. """ - self.putcmd("helo", _get_fqdn_hostname(name)) + if name: + self.putcmd("helo", name) + else: + self.putcmd("helo", make_fqdn()) (code,msg)=self.getreply() self.helo_resp=msg return (code,msg) @@ -316,7 +327,10 @@ class SMTP: Hostname to send for this command defaults to the FQDN of the local host. """ - self.putcmd("ehlo", _get_fqdn_hostname(name)) + if name: + self.putcmd("ehlo", name) + else: + self.putcmd("ehlo", make_fqdn()) (code,msg)=self.getreply() # According to RFC1869 some (badly written) # MTA's will disconnect on an ehlo. Toss an exception if -- cgit v0.12