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

Side by Side Diff: Lib/ftplib.py

Issue 19689: ssl.create_default_context()
Patch Set: Created 5 years, 3 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/asyncio/selector_events.py ('k') | Lib/http/client.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 """An FTP client class and some helper functions. 1 """An FTP client class and some helper functions.
2 2
3 Based on RFC 959: File Transfer Protocol (FTP), by J. Postel and J. Reynolds 3 Based on RFC 959: File Transfer Protocol (FTP), by J. Postel and J. Reynolds
4 4
5 Example: 5 Example:
6 6
7 >>> from ftplib import FTP 7 >>> from ftplib import FTP
8 >>> ftp = FTP('ftp.python.org') # connect to host, default port 8 >>> ftp = FTP('ftp.python.org') # connect to host, default port
9 >>> ftp.login() # default, i.e.: user anonymous, passwd anonymous@ 9 >>> ftp.login() # default, i.e.: user anonymous, passwd anonymous@
10 '230 Guest login ok, access restrictions apply.' 10 '230 Guest login ok, access restrictions apply.'
(...skipping 709 matching lines...) Expand 10 before | Expand all | Expand 10 after
720 certfile=None, context=None, 720 certfile=None, context=None,
721 timeout=_GLOBAL_DEFAULT_TIMEOUT, source_address=None): 721 timeout=_GLOBAL_DEFAULT_TIMEOUT, source_address=None):
722 if context is not None and keyfile is not None: 722 if context is not None and keyfile is not None:
723 raise ValueError("context and keyfile arguments are mutually " 723 raise ValueError("context and keyfile arguments are mutually "
724 "exclusive") 724 "exclusive")
725 if context is not None and certfile is not None: 725 if context is not None and certfile is not None:
726 raise ValueError("context and certfile arguments are mutually " 726 raise ValueError("context and certfile arguments are mutually "
727 "exclusive") 727 "exclusive")
728 self.keyfile = keyfile 728 self.keyfile = keyfile
729 self.certfile = certfile 729 self.certfile = certfile
730 if context is None:
731 context = ssl.create_default_context(self.ssl_version,
732 certfile=certfile,
733 keyfile=keyfile)
730 self.context = context 734 self.context = context
731 self._prot_p = False 735 self._prot_p = False
732 FTP.__init__(self, host, user, passwd, acct, timeout, source_address ) 736 FTP.__init__(self, host, user, passwd, acct, timeout, source_address )
733 737
734 def login(self, user='', passwd='', acct='', secure=True): 738 def login(self, user='', passwd='', acct='', secure=True):
735 if secure and not isinstance(self.sock, ssl.SSLSocket): 739 if secure and not isinstance(self.sock, ssl.SSLSocket):
736 self.auth() 740 self.auth()
737 return FTP.login(self, user, passwd, acct) 741 return FTP.login(self, user, passwd, acct)
738 742
739 def auth(self): 743 def auth(self):
740 '''Set up secure control connection by using TLS/SSL.''' 744 '''Set up secure control connection by using TLS/SSL.'''
741 if isinstance(self.sock, ssl.SSLSocket): 745 if isinstance(self.sock, ssl.SSLSocket):
742 raise ValueError("Already using TLS") 746 raise ValueError("Already using TLS")
743 if self.ssl_version == ssl.PROTOCOL_TLSv1: 747 if self.ssl_version == ssl.PROTOCOL_TLSv1:
744 resp = self.voidcmd('AUTH TLS') 748 resp = self.voidcmd('AUTH TLS')
745 else: 749 else:
746 resp = self.voidcmd('AUTH SSL') 750 resp = self.voidcmd('AUTH SSL')
747 if self.context is not None: 751 self.sock = self.context.wrap_socket(self.sock)
748 self.sock = self.context.wrap_socket(self.sock)
749 else:
750 self.sock = ssl.wrap_socket(self.sock, self.keyfile,
751 self.certfile,
752 ssl_version=self.ssl_version)
753 self.file = self.sock.makefile(mode='r', encoding=self.encoding) 752 self.file = self.sock.makefile(mode='r', encoding=self.encoding)
754 return resp 753 return resp
755 754
756 def ccc(self): 755 def ccc(self):
757 '''Switch back to a clear-text control connection.''' 756 '''Switch back to a clear-text control connection.'''
758 if not isinstance(self.sock, ssl.SSLSocket): 757 if not isinstance(self.sock, ssl.SSLSocket):
759 raise ValueError("not using TLS") 758 raise ValueError("not using TLS")
760 resp = self.voidcmd('CCC') 759 resp = self.voidcmd('CCC')
761 self.sock = self.sock.unwrap() 760 self.sock = self.sock.unwrap()
762 return resp 761 return resp
(...skipping 18 matching lines...) Expand all
781 '''Set up clear text data connection.''' 780 '''Set up clear text data connection.'''
782 resp = self.voidcmd('PROT C') 781 resp = self.voidcmd('PROT C')
783 self._prot_p = False 782 self._prot_p = False
784 return resp 783 return resp
785 784
786 # --- Overridden FTP methods 785 # --- Overridden FTP methods
787 786
788 def ntransfercmd(self, cmd, rest=None): 787 def ntransfercmd(self, cmd, rest=None):
789 conn, size = FTP.ntransfercmd(self, cmd, rest) 788 conn, size = FTP.ntransfercmd(self, cmd, rest)
790 if self._prot_p: 789 if self._prot_p:
791 if self.context is not None: 790 conn = self.context.wrap_socket(conn)
792 conn = self.context.wrap_socket(conn)
793 else:
794 conn = ssl.wrap_socket(conn, self.keyfile, self.certfile,
795 ssl_version=self.ssl_version)
796 return conn, size 791 return conn, size
797 792
798 def abort(self): 793 def abort(self):
799 # overridden as we can't pass MSG_OOB flag to sendall() 794 # overridden as we can't pass MSG_OOB flag to sendall()
800 line = b'ABOR' + B_CRLF 795 line = b'ABOR' + B_CRLF
801 self.sock.sendall(line) 796 self.sock.sendall(line)
802 resp = self.getmultiline() 797 resp = self.getmultiline()
803 if resp[:3] not in {'426', '225', '226'}: 798 if resp[:3] not in {'426', '225', '226'}:
804 raise error_proto(resp) 799 raise error_proto(resp)
805 return resp 800 return resp
(...skipping 275 matching lines...) Expand 10 before | Expand all | Expand 10 after
1081 elif file == '-p': 1076 elif file == '-p':
1082 ftp.set_pasv(not ftp.passiveserver) 1077 ftp.set_pasv(not ftp.passiveserver)
1083 else: 1078 else:
1084 ftp.retrbinary('RETR ' + file, \ 1079 ftp.retrbinary('RETR ' + file, \
1085 sys.stdout.write, 1024) 1080 sys.stdout.write, 1024)
1086 ftp.quit() 1081 ftp.quit()
1087 1082
1088 1083
1089 if __name__ == '__main__': 1084 if __name__ == '__main__':
1090 test() 1085 test()
OLDNEW
« no previous file with comments | « Lib/asyncio/selector_events.py ('k') | Lib/http/client.py » ('j') | no next file with comments »

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