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

Side by Side Diff: Lib/poplib.py

Issue 19689: ssl.create_default_context()
Patch Set: Created 5 years, 4 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/nntplib.py ('k') | Lib/smtplib.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 """A POP3 client class. 1 """A POP3 client class.
2 2
3 Based on the J. Myers POP3 draft, Jan. 96 3 Based on the J. Myers POP3 draft, Jan. 96
4 """ 4 """
5 5
6 # Author: David Ascher <david_ascher@brown.edu> 6 # Author: David Ascher <david_ascher@brown.edu>
7 # [heavily stealing from nntplib.py] 7 # [heavily stealing from nntplib.py]
8 # Updated: Piers Lauder <piers@cs.su.oz.au> [Jul '97] 8 # Updated: Piers Lauder <piers@cs.su.oz.au> [Jul '97]
9 # String method conversion and test jig improvements by ESR, February 2001. 9 # String method conversion and test jig improvements by ESR, February 2001.
10 # Added the POP3_SSL class. Methods loosely based on IMAP_SSL. Hector Urtubia <u rtubia@mrbook.org> Aug 2003 10 # Added the POP3_SSL class. Methods loosely based on IMAP_SSL. Hector Urtubia <u rtubia@mrbook.org> Aug 2003
(...skipping 367 matching lines...) Expand 10 before | Expand all | Expand 10 after
378 context - a ssl.SSLContext 378 context - a ssl.SSLContext
379 """ 379 """
380 if not HAVE_SSL: 380 if not HAVE_SSL:
381 raise error_proto('-ERR TLS support missing') 381 raise error_proto('-ERR TLS support missing')
382 if self._tls_established: 382 if self._tls_established:
383 raise error_proto('-ERR TLS session already established') 383 raise error_proto('-ERR TLS session already established')
384 caps = self.capa() 384 caps = self.capa()
385 if not 'STLS' in caps: 385 if not 'STLS' in caps:
386 raise error_proto('-ERR STLS not supported by server') 386 raise error_proto('-ERR STLS not supported by server')
387 if context is None: 387 if context is None:
388 context = ssl.SSLContext(ssl.PROTOCOL_SSLv23) 388 context = ssl.create_default_context()
389 context.options |= ssl.OP_NO_SSLv2
390 resp = self._shortcmd('STLS') 389 resp = self._shortcmd('STLS')
391 self.sock = context.wrap_socket(self.sock) 390 self.sock = context.wrap_socket(self.sock)
392 self.file = self.sock.makefile('rb') 391 self.file = self.sock.makefile('rb')
393 self._tls_established = True 392 self._tls_established = True
394 return resp 393 return resp
395 394
396 395
397 if HAVE_SSL: 396 if HAVE_SSL:
398 397
399 class POP3_SSL(POP3): 398 class POP3_SSL(POP3):
(...skipping 14 matching lines...) Expand all
414 def __init__(self, host, port=POP3_SSL_PORT, keyfile=None, certfile=None , 413 def __init__(self, host, port=POP3_SSL_PORT, keyfile=None, certfile=None ,
415 timeout=socket._GLOBAL_DEFAULT_TIMEOUT, context=None): 414 timeout=socket._GLOBAL_DEFAULT_TIMEOUT, context=None):
416 if context is not None and keyfile is not None: 415 if context is not None and keyfile is not None:
417 raise ValueError("context and keyfile arguments are mutually " 416 raise ValueError("context and keyfile arguments are mutually "
418 "exclusive") 417 "exclusive")
419 if context is not None and certfile is not None: 418 if context is not None and certfile is not None:
420 raise ValueError("context and certfile arguments are mutually " 419 raise ValueError("context and certfile arguments are mutually "
421 "exclusive") 420 "exclusive")
422 self.keyfile = keyfile 421 self.keyfile = keyfile
423 self.certfile = certfile 422 self.certfile = certfile
423 if context is None:
424 context = ssl.create_default_context(certfile=certfile,
425 keyfile=keyfile)
424 self.context = context 426 self.context = context
425 POP3.__init__(self, host, port, timeout) 427 POP3.__init__(self, host, port, timeout)
426 428
427 def _create_socket(self, timeout): 429 def _create_socket(self, timeout):
428 sock = POP3._create_socket(self, timeout) 430 sock = POP3._create_socket(self, timeout)
429 if self.context is not None: 431 sock = self.context.wrap_socket(sock)
430 sock = self.context.wrap_socket(sock)
431 else:
432 sock = ssl.wrap_socket(sock, self.keyfile, self.certfile)
433 return sock 432 return sock
434 433
435 def stls(self, keyfile=None, certfile=None, context=None): 434 def stls(self, keyfile=None, certfile=None, context=None):
436 """The method unconditionally raises an exception since the 435 """The method unconditionally raises an exception since the
437 STLS command doesn't make any sense on an already established 436 STLS command doesn't make any sense on an already established
438 SSL/TLS session. 437 SSL/TLS session.
439 """ 438 """
440 raise error_proto('-ERR TLS session already established') 439 raise error_proto('-ERR TLS session already established')
441 440
442 __all__.append("POP3_SSL") 441 __all__.append("POP3_SSL")
443 442
444 if __name__ == "__main__": 443 if __name__ == "__main__":
445 import sys 444 import sys
446 a = POP3(sys.argv[1]) 445 a = POP3(sys.argv[1])
447 print(a.getwelcome()) 446 print(a.getwelcome())
448 a.user(sys.argv[2]) 447 a.user(sys.argv[2])
449 a.pass_(sys.argv[3]) 448 a.pass_(sys.argv[3])
450 a.list() 449 a.list()
451 (numMsgs, totalSize) = a.stat() 450 (numMsgs, totalSize) = a.stat()
452 for i in range(1, numMsgs + 1): 451 for i in range(1, numMsgs + 1):
453 (header, msg, octets) = a.retr(i) 452 (header, msg, octets) = a.retr(i)
454 print("Message %d:" % i) 453 print("Message %d:" % i)
455 for line in msg: 454 for line in msg:
456 print(' ' + line) 455 print(' ' + line)
457 print('-----------------------') 456 print('-----------------------')
458 a.quit() 457 a.quit()
OLDNEW
« no previous file with comments | « Lib/nntplib.py ('k') | Lib/smtplib.py » ('j') | no next file with comments »

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