Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code | Sign in
(2664)

Side by Side Diff: Lib/smtplib.py

Issue 19689: ssl.create_default_context()
Patch Set: Created 5 years, 6 months ago
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments. Please Sign in to add in-line comments.
Jump to:
View unified diff | Download patch
« no previous file with comments | « Lib/poplib.py ('k') | Lib/ssl.py » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 #! /usr/bin/env python3 1 #! /usr/bin/env python3
2 2
3 '''SMTP/ESMTP client class. 3 '''SMTP/ESMTP client class.
4 4
5 This should follow RFC 821 (SMTP), RFC 1869 (ESMTP), RFC 2554 (SMTP 5 This should follow RFC 821 (SMTP), RFC 1869 (ESMTP), RFC 2554 (SMTP
6 Authentication) and RFC 2487 (Secure SMTP over TLS). 6 Authentication) and RFC 2487 (Secure SMTP over TLS).
7 7
8 Notes: 8 Notes:
9 9
10 Please remember, when doing ESMTP, that the names of the SMTP service 10 Please remember, when doing ESMTP, that the names of the SMTP service
(...skipping 646 matching lines...) Expand 10 before | Expand all | Expand 10 after
657 (resp, reply) = self.docmd("STARTTLS") 657 (resp, reply) = self.docmd("STARTTLS")
658 if resp == 220: 658 if resp == 220:
659 if not _have_ssl: 659 if not _have_ssl:
660 raise RuntimeError("No SSL support included in this Python") 660 raise RuntimeError("No SSL support included in this Python")
661 if context is not None and keyfile is not None: 661 if context is not None and keyfile is not None:
662 raise ValueError("context and keyfile arguments are mutually " 662 raise ValueError("context and keyfile arguments are mutually "
663 "exclusive") 663 "exclusive")
664 if context is not None and certfile is not None: 664 if context is not None and certfile is not None:
665 raise ValueError("context and certfile arguments are mutually " 665 raise ValueError("context and certfile arguments are mutually "
666 "exclusive") 666 "exclusive")
667 if context is not None: 667 if context is None:
668 self.sock = context.wrap_socket(self.sock) 668 context = ssl.create_default_context(certfile=certfile,
669 else: 669 keyfile=keyfile)
670 self.sock = ssl.wrap_socket(self.sock, keyfile, certfile) 670 self.sock = context.wrap_socket(self.sock)
671 self.file = None 671 self.file = None
672 # RFC 3207: 672 # RFC 3207:
673 # The client MUST discard any knowledge obtained from 673 # The client MUST discard any knowledge obtained from
674 # the server, such as the list of SMTP service extensions, 674 # the server, such as the list of SMTP service extensions,
675 # which was not obtained from the TLS negotiation itself. 675 # which was not obtained from the TLS negotiation itself.
676 self.helo_resp = None 676 self.helo_resp = None
677 self.ehlo_resp = None 677 self.ehlo_resp = None
678 self.esmtp_features = {} 678 self.esmtp_features = {}
679 self.does_esmtp = 0 679 self.does_esmtp = 0
680 return (resp, reply) 680 return (resp, reply)
(...skipping 192 matching lines...) Expand 10 before | Expand all | Expand 10 after
873 timeout=socket._GLOBAL_DEFAULT_TIMEOUT, 873 timeout=socket._GLOBAL_DEFAULT_TIMEOUT,
874 source_address=None, context=None): 874 source_address=None, context=None):
875 if context is not None and keyfile is not None: 875 if context is not None and keyfile is not None:
876 raise ValueError("context and keyfile arguments are mutually " 876 raise ValueError("context and keyfile arguments are mutually "
877 "exclusive") 877 "exclusive")
878 if context is not None and certfile is not None: 878 if context is not None and certfile is not None:
879 raise ValueError("context and certfile arguments are mutually " 879 raise ValueError("context and certfile arguments are mutually "
880 "exclusive") 880 "exclusive")
881 self.keyfile = keyfile 881 self.keyfile = keyfile
882 self.certfile = certfile 882 self.certfile = certfile
883 if context is None:
884 context = ssl.create_default_context(certfile=self.certfile,
885 keyfile=self.keyfile)
883 self.context = context 886 self.context = context
884 SMTP.__init__(self, host, port, local_hostname, timeout, 887 SMTP.__init__(self, host, port, local_hostname, timeout,
885 source_address) 888 source_address)
886 889
887 def _get_socket(self, host, port, timeout): 890 def _get_socket(self, host, port, timeout):
888 if self.debuglevel > 0: 891 if self.debuglevel > 0:
889 print('connect:', (host, port), file=stderr) 892 print('connect:', (host, port), file=stderr)
890 new_socket = socket.create_connection((host, port), timeout, 893 new_socket = socket.create_connection((host, port), timeout,
891 self.source_address) 894 self.source_address)
892 if self.context is not None: 895 new_socket = self.context.wrap_socket(new_socket)
893 new_socket = self.context.wrap_socket(new_socket)
894 else:
895 new_socket = ssl.wrap_socket(new_socket, self.keyfile, self.cert file)
896 return new_socket 896 return new_socket
897 897
898 __all__.append("SMTP_SSL") 898 __all__.append("SMTP_SSL")
899 899
900 # 900 #
901 # LMTP extension 901 # LMTP extension
902 # 902 #
903 LMTP_PORT = 2003 903 LMTP_PORT = 2003
904 904
905 class LMTP(SMTP): 905 class LMTP(SMTP):
(...skipping 59 matching lines...) Expand 10 before | Expand all | Expand 10 after
965 line = sys.stdin.readline() 965 line = sys.stdin.readline()
966 if not line: 966 if not line:
967 break 967 break
968 msg = msg + line 968 msg = msg + line
969 print("Message length is %d" % len(msg)) 969 print("Message length is %d" % len(msg))
970 970
971 server = SMTP('localhost') 971 server = SMTP('localhost')
972 server.set_debuglevel(1) 972 server.set_debuglevel(1)
973 server.sendmail(fromaddr, toaddrs, msg) 973 server.sendmail(fromaddr, toaddrs, msg)
974 server.quit() 974 server.quit()
OLDNEW
« no previous file with comments | « Lib/poplib.py ('k') | Lib/ssl.py » ('j') | no next file with comments »

RSS Feeds Recent Issues | This issue
This is Rietveld 894c83f36cb7+