{"id":1032200,"date":"2024-12-31T11:31:42","date_gmt":"2024-12-31T03:31:42","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1032200.html"},"modified":"2024-12-31T11:31:44","modified_gmt":"2024-12-31T03:31:44","slug":"python%e5%a6%82%e4%bd%95%e5%ae%9e%e7%8e%b0%e5%af%b9ftp%e5%8a%a0%e5%af%86%e4%bc%a0%e8%be%93","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1032200.html","title":{"rendered":"python\u5982\u4f55\u5b9e\u73b0\u5bf9FTP\u52a0\u5bc6\u4f20\u8f93"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-docs.pingcode.com\/wp-content\/uploads\/2024\/12\/aee3575c-9c36-4f98-a662-6d0322c7e8db.webp?x-oss-process=image\/auto-orient,1\/format,webp\" alt=\"python\u5982\u4f55\u5b9e\u73b0\u5bf9FTP\u52a0\u5bc6\u4f20\u8f93\" \/><\/p>\n<p><p> <strong>Python\u5b9e\u73b0FTP\u52a0\u5bc6\u4f20\u8f93\u7684\u65b9\u6cd5\u5305\u62ec\u4f7f\u7528ftplib\u6a21\u5757\u3001\u4f7f\u7528\u7b2c\u4e09\u65b9\u5e93\u5982paramiko\u3001\u914d\u7f6eSSL\/TLS\u52a0\u5bc6\u4f20\u8f93\u3002<\/strong>\u5176\u4e2d\uff0c\u4f7f\u7528\u7b2c\u4e09\u65b9\u5e93\u5982paramiko\u5b9e\u73b0SFTP\u4f20\u8f93\u662f\u4e00\u79cd\u5e38\u89c1\u4e14\u5b89\u5168\u7684\u65b9\u5f0f\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u4f7f\u7528ftplib\u6a21\u5757<\/h3>\n<\/p>\n<p><p><code>ftplib<\/code>\u6a21\u5757\u662fPython\u6807\u51c6\u5e93\u7684\u4e00\u90e8\u5206\uff0c\u5b83\u63d0\u4f9b\u4e86FTP\u534f\u8bae\u7684\u652f\u6301\u3002\u4f46\u662f\uff0c<code>ftplib<\/code>\u6a21\u5757\u672c\u8eab\u4e0d\u652f\u6301\u52a0\u5bc6\u4f20\u8f93\u3002\u4e3a\u4e86\u89e3\u51b3\u8fd9\u4e2a\u95ee\u9898\uff0c\u53ef\u4ee5\u4f7f\u7528<code>ftplib<\/code>\u7ed3\u5408<code>ssl<\/code>\u6a21\u5757\u5b9e\u73b0FTP\u7684SSL\/TLS\u52a0\u5bc6\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import ftplib<\/p>\n<p>import ssl<\/p>\n<h2><strong>\u521b\u5efaSSL\u4e0a\u4e0b\u6587<\/strong><\/h2>\n<p>context = ssl.create_default_context()<\/p>\n<h2><strong>\u521b\u5efaFTP_TLS\u5bf9\u8c61<\/strong><\/h2>\n<p>ftp = ftplib.FTP_TLS(context=context)<\/p>\n<h2><strong>\u8fde\u63a5\u5230FTP\u670d\u52a1\u5668<\/strong><\/h2>\n<p>ftp.connect(&#39;ftp.example.com&#39;, 21)<\/p>\n<h2><strong>\u767b\u5f55<\/strong><\/h2>\n<p>ftp.login(&#39;username&#39;, &#39;password&#39;)<\/p>\n<h2><strong>\u8bbe\u7f6e\u52a0\u5bc6\u4f20\u8f93<\/strong><\/h2>\n<p>ftp.prot_p()<\/p>\n<h2><strong>\u6267\u884cFTP\u64cd\u4f5c\uff0c\u4f8b\u5982\u5217\u51fa\u76ee\u5f55\u5185\u5bb9<\/strong><\/h2>\n<p>ftp.retrlines(&#39;LIST&#39;)<\/p>\n<h2><strong>\u5173\u95ed\u8fde\u63a5<\/strong><\/h2>\n<p>ftp.quit()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e8c\u3001\u4f7f\u7528paramiko\u5e93<\/h3>\n<\/p>\n<p><p><code>paramiko<\/code>\u662f\u4e00\u4e2a\u7b2c\u4e09\u65b9\u5e93\uff0c\u63d0\u4f9b\u4e86SSHv2\u534f\u8bae\u7684\u652f\u6301\uff0c\u53ef\u4ee5\u7528\u4e8e\u5b9e\u73b0SFTP\uff08SSH File Transfer Protocol\uff09\u52a0\u5bc6\u4f20\u8f93\u3002<code>paramiko<\/code>\u5e93\u6bd4<code>ftplib<\/code>\u66f4\u5f3a\u5927\uff0c\u5c24\u5176\u5728\u5b89\u5168\u6027\u65b9\u9762\u3002<\/p>\n<\/p>\n<p><h4>\u5b89\u88c5paramiko<\/h4>\n<\/p>\n<p><p>\u9996\u5148\uff0c\u5b89\u88c5<code>paramiko<\/code>\u5e93\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pip install paramiko<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>\u4f7f\u7528paramiko\u5b9e\u73b0SFTP<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">import paramiko<\/p>\n<h2><strong>\u521b\u5efaSSH\u5ba2\u6237\u7aef<\/strong><\/h2>\n<p>ssh = paramiko.SSHClient()<\/p>\n<h2><strong>\u81ea\u52a8\u6dfb\u52a0\u4e3b\u673a\u5bc6\u94a5<\/strong><\/h2>\n<p>ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())<\/p>\n<h2><strong>\u8fde\u63a5\u5230SFTP\u670d\u52a1\u5668<\/strong><\/h2>\n<p>ssh.connect(&#39;sftp.example.com&#39;, username=&#39;username&#39;, password=&#39;password&#39;)<\/p>\n<h2><strong>\u521b\u5efaSFTP\u4f1a\u8bdd<\/strong><\/h2>\n<p>sftp = ssh.open_sftp()<\/p>\n<h2><strong>\u4e0a\u4f20\u6587\u4ef6<\/strong><\/h2>\n<p>sftp.put(&#39;local_file.txt&#39;, &#39;remote_file.txt&#39;)<\/p>\n<h2><strong>\u4e0b\u8f7d\u6587\u4ef6<\/strong><\/h2>\n<p>sftp.get(&#39;remote_file.txt&#39;, &#39;local_file.txt&#39;)<\/p>\n<h2><strong>\u5173\u95edSFTP\u4f1a\u8bdd<\/strong><\/h2>\n<p>sftp.close()<\/p>\n<h2><strong>\u5173\u95edSSH\u8fde\u63a5<\/strong><\/h2>\n<p>ssh.close()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e09\u3001\u914d\u7f6eSSL\/TLS\u52a0\u5bc6\u4f20\u8f93<\/h3>\n<\/p>\n<p><p>\u914d\u7f6eSSL\/TLS\u52a0\u5bc6\u4f20\u8f93\u53ef\u4ee5\u901a\u8fc7\u4f7f\u7528FTP\u670d\u52a1\u5668\u548c\u5ba2\u6237\u7aef\u7684SSL\/TLS\u529f\u80fd\u6765\u5b9e\u73b0\u3002\u4ee5vsftpd\uff08Very Secure FTP Daemon\uff09\u4e3a\u4f8b\uff0c\u4ee5\u4e0b\u662f\u914d\u7f6e\u6b65\u9aa4\uff1a<\/p>\n<\/p>\n<p><h4>1\u3001\u5b89\u88c5vsftpd<\/h4>\n<\/p>\n<p><pre><code class=\"language-bash\">sudo apt-get install vsftpd<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2\u3001\u751f\u6210SSL\u8bc1\u4e66<\/h4>\n<\/p>\n<p><pre><code class=\"language-bash\">sudo openssl req -x509 -nodes -days 365 -newkey rsa:2048 -keyout \/etc\/ssl\/private\/vsftpd.pem -out \/etc\/ssl\/private\/vsftpd.pem<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>3\u3001\u914d\u7f6evsftpd<\/h4>\n<\/p>\n<p><p>\u7f16\u8f91<code>\/etc\/vsftpd.conf<\/code>\u6587\u4ef6\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\"># \u542f\u7528SSL<\/p>\n<p>ssl_enable=YES<\/p>\n<h2><strong>\u6307\u5b9aSSL\u8bc1\u4e66\u548c\u5bc6\u94a5<\/strong><\/h2>\n<p>rsa_cert_file=\/etc\/ssl\/private\/vsftpd.pem<\/p>\n<p>rsa_private_key_file=\/etc\/ssl\/private\/vsftpd.pem<\/p>\n<h2><strong>\u5141\u8bb8\u533f\u540d\u7528\u6237\u4f7f\u7528SSL<\/strong><\/h2>\n<p>allow_anon_ssl=NO<\/p>\n<h2><strong>\u5f3a\u5236\u4f7f\u7528SSL<\/strong><\/h2>\n<p>force_local_data_ssl=YES<\/p>\n<p>force_local_logins_ssl=YES<\/p>\n<h2><strong>\u5176\u4ed6\u914d\u7f6e\u9009\u9879<\/strong><\/h2>\n<p>ssl_tlsv1=YES<\/p>\n<p>ssl_sslv2=NO<\/p>\n<p>ssl_sslv3=NO<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u91cd\u542fvsftpd\u670d\u52a1\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">sudo systemctl restart vsftpd<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u56db\u3001\u7ed3\u5408ftplib\u548cssl\u6a21\u5757\u5b9e\u73b0FTPS<\/h3>\n<\/p>\n<p><p>\u53ef\u4ee5\u5c06\u524d\u6587\u63d0\u5230\u7684<code>ftplib<\/code>\u548c<code>ssl<\/code>\u6a21\u5757\u7ed3\u5408\u4f7f\u7528\uff0c\u6765\u5b9e\u73b0FTPS\uff08FTP Secure\uff09\u4f20\u8f93\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import ftplib<\/p>\n<p>import ssl<\/p>\n<p>def ftp_secure_connect(host, port, username, password):<\/p>\n<p>    # \u521b\u5efaSSL\u4e0a\u4e0b\u6587<\/p>\n<p>    context = ssl.create_default_context()<\/p>\n<p>    # \u521b\u5efaFTP_TLS\u5bf9\u8c61<\/p>\n<p>    ftp = ftplib.FTP_TLS(context=context)<\/p>\n<p>    # \u8fde\u63a5\u5230FTP\u670d\u52a1\u5668<\/p>\n<p>    ftp.connect(host, port)<\/p>\n<p>    # \u767b\u5f55<\/p>\n<p>    ftp.login(username, password)<\/p>\n<p>    # \u8bbe\u7f6e\u52a0\u5bc6\u4f20\u8f93<\/p>\n<p>    ftp.prot_p()<\/p>\n<p>    return ftp<\/p>\n<p>ftp = ftp_secure_connect(&#39;ftp.example.com&#39;, 21, &#39;username&#39;, &#39;password&#39;)<\/p>\n<h2><strong>\u6267\u884cFTP\u64cd\u4f5c\uff0c\u4f8b\u5982\u5217\u51fa\u76ee\u5f55\u5185\u5bb9<\/strong><\/h2>\n<p>ftp.retrlines(&#39;LIST&#39;)<\/p>\n<h2><strong>\u5173\u95ed\u8fde\u63a5<\/strong><\/h2>\n<p>ftp.quit()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e94\u3001\u4f7f\u7528\u7b2c\u4e09\u65b9\u5e93pysftp<\/h3>\n<\/p>\n<p><p><code>pysftp<\/code>\u662f\u53e6\u4e00\u4e2a\u53ef\u4ee5\u7528\u4e8e\u5b9e\u73b0SFTP\u52a0\u5bc6\u4f20\u8f93\u7684\u7b2c\u4e09\u65b9\u5e93\u3002\u5b83\u5c01\u88c5\u4e86paramiko\u5e93\uff0c\u63d0\u4f9b\u4e86\u66f4\u7b80\u6d01\u7684\u63a5\u53e3\u3002<\/p>\n<\/p>\n<p><h4>\u5b89\u88c5pysftp<\/h4>\n<\/p>\n<p><p>\u9996\u5148\uff0c\u5b89\u88c5<code>pysftp<\/code>\u5e93\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pip install pysftp<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>\u4f7f\u7528pysftp\u5b9e\u73b0SFTP<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">import pysftp<\/p>\n<h2><strong>\u5b9a\u4e49SFTP\u670d\u52a1\u5668\u4fe1\u606f<\/strong><\/h2>\n<p>hostname = &#39;sftp.example.com&#39;<\/p>\n<p>username = &#39;username&#39;<\/p>\n<p>password = &#39;password&#39;<\/p>\n<h2><strong>\u521b\u5efaSFTP\u8fde\u63a5<\/strong><\/h2>\n<p>with pysftp.Connection(hostname, username=username, password=password) as sftp:<\/p>\n<p>    # \u5207\u6362\u5230\u76ee\u6807\u76ee\u5f55<\/p>\n<p>    sftp.cwd(&#39;\/path\/to\/directory&#39;)<\/p>\n<p>    # \u4e0a\u4f20\u6587\u4ef6<\/p>\n<p>    sftp.put(&#39;local_file.txt&#39;)<\/p>\n<p>    # \u4e0b\u8f7d\u6587\u4ef6<\/p>\n<p>    sftp.get(&#39;remote_file.txt&#39;, &#39;local_file.txt&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u516d\u3001\u4f7f\u7528pycurl\u5e93<\/h3>\n<\/p>\n<p><p><code>pycurl<\/code>\u5e93\u662flibcurl\u7684Python\u63a5\u53e3\uff0c\u53ef\u4ee5\u7528\u4e8e\u5b9e\u73b0FTP\u7684\u52a0\u5bc6\u4f20\u8f93\u3002<code>pycurl<\/code>\u5e93\u652f\u6301\u591a\u79cd\u534f\u8bae\uff0c\u5305\u62ecFTP\u3001FTPS\u548cSFTP\u3002<\/p>\n<\/p>\n<p><h4>\u5b89\u88c5pycurl<\/h4>\n<\/p>\n<p><p>\u9996\u5148\uff0c\u5b89\u88c5<code>pycurl<\/code>\u5e93\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pip install pycurl<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>\u4f7f\u7528pycurl\u5b9e\u73b0FTPS<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">import pycurl<\/p>\n<p>from io import BytesIO<\/p>\n<h2><strong>\u5b9a\u4e49FTPS\u670d\u52a1\u5668\u4fe1\u606f<\/strong><\/h2>\n<p>url = &#39;ftps:\/\/ftp.example.com\/remote_file.txt&#39;<\/p>\n<p>username = &#39;username&#39;<\/p>\n<p>password = &#39;password&#39;<\/p>\n<h2><strong>\u521b\u5efapycurl\u5bf9\u8c61<\/strong><\/h2>\n<p>curl = pycurl.Curl()<\/p>\n<h2><strong>\u8bbe\u7f6eFTPS URL<\/strong><\/h2>\n<p>curl.setopt(pycurl.URL, url)<\/p>\n<h2><strong>\u8bbe\u7f6e\u7528\u6237\u540d\u548c\u5bc6\u7801<\/strong><\/h2>\n<p>curl.setopt(pycurl.USERPWD, f&#39;{username}:{password}&#39;)<\/p>\n<h2><strong>\u542f\u7528SSL<\/strong><\/h2>\n<p>curl.setopt(pycurl.USE_SSL, pycurl.SSL_ALL)<\/p>\n<h2><strong>\u5ffd\u7565SSL\u8bc1\u4e66\u9a8c\u8bc1\uff08\u6839\u636e\u9700\u8981\u914d\u7f6e\uff09<\/strong><\/h2>\n<p>curl.setopt(pycurl.SSL_VERIFYPEER, 0)<\/p>\n<p>curl.setopt(pycurl.SSL_VERIFYHOST, 0)<\/p>\n<h2><strong>\u4e0b\u8f7d\u6587\u4ef6\u5185\u5bb9<\/strong><\/h2>\n<p>buffer = BytesIO()<\/p>\n<p>curl.setopt(pycurl.WRITEDATA, buffer)<\/p>\n<p>curl.perform()<\/p>\n<h2><strong>\u83b7\u53d6HTTP\u54cd\u5e94\u4ee3\u7801<\/strong><\/h2>\n<p>response_code = curl.getinfo(pycurl.RESPONSE_CODE)<\/p>\n<p>print(f&#39;HTTP response code: {response_code}&#39;)<\/p>\n<h2><strong>\u5173\u95edpycurl\u5bf9\u8c61<\/strong><\/h2>\n<p>curl.close()<\/p>\n<h2><strong>\u8f93\u51fa\u6587\u4ef6\u5185\u5bb9<\/strong><\/h2>\n<p>print(buffer.getvalue().decode(&#39;utf-8&#39;))<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e03\u3001\u4f7f\u7528asyncio\u548c<a href=\"https:\/\/docs.pingcode.com\/blog\/59162.html\" target=\"_blank\">AI<\/a>oftp\u5e93<\/h3>\n<\/p>\n<p><p><code>aioftp<\/code>\u662f\u4e00\u4e2a\u57fa\u4e8e<code>asyncio<\/code>\u7684\u5f02\u6b65FTP\u5ba2\u6237\u7aef\u5e93\uff0c\u53ef\u4ee5\u7528\u4e8e\u5b9e\u73b0FTP\u7684\u52a0\u5bc6\u4f20\u8f93\u3002<\/p>\n<\/p>\n<p><h4>\u5b89\u88c5aioftp<\/h4>\n<\/p>\n<p><p>\u9996\u5148\uff0c\u5b89\u88c5<code>aioftp<\/code>\u5e93\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pip install aioftp<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>\u4f7f\u7528aioftp\u5b9e\u73b0FTPS<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">import asyncio<\/p>\n<p>import aioftp<\/p>\n<p>async def main():<\/p>\n<p>    # \u5b9a\u4e49FTPS\u670d\u52a1\u5668\u4fe1\u606f<\/p>\n<p>    host = &#39;ftp.example.com&#39;<\/p>\n<p>    port = 21<\/p>\n<p>    user = &#39;username&#39;<\/p>\n<p>    password = &#39;password&#39;<\/p>\n<p>    # \u521b\u5efaFTP\u5ba2\u6237\u7aef<\/p>\n<p>    async with aioftp.Client.context(host, port, user, password, ssl=True) as client:<\/p>\n<p>        # \u5217\u51fa\u76ee\u5f55\u5185\u5bb9<\/p>\n<p>        async for path in client.list(&quot;\/&quot;):<\/p>\n<p>            print(path)<\/p>\n<p>        # \u4e0b\u8f7d\u6587\u4ef6<\/p>\n<p>        await client.download(&quot;remote_file.txt&quot;, &quot;local_file.txt&quot;)<\/p>\n<p>        # \u4e0a\u4f20\u6587\u4ef6<\/p>\n<p>        await client.upload(&quot;local_file.txt&quot;, &quot;remote_file.txt&quot;)<\/p>\n<h2><strong>\u8fd0\u884c\u5f02\u6b65\u4efb\u52a1<\/strong><\/h2>\n<p>asyncio.run(main())<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u516b\u3001\u603b\u7ed3<\/h3>\n<\/p>\n<p><p>\u5b9e\u73b0FTP\u52a0\u5bc6\u4f20\u8f93\u7684\u65b9\u6cd5\u6709\u5f88\u591a\u79cd\uff0c\u5305\u62ec\u4f7f\u7528Python\u6807\u51c6\u5e93<code>ftplib<\/code>\u7ed3\u5408<code>ssl<\/code>\u6a21\u5757\u3001\u4f7f\u7528\u7b2c\u4e09\u65b9\u5e93<code>paramiko<\/code>\u5b9e\u73b0SFTP\u3001\u914d\u7f6eFTP\u670d\u52a1\u5668\u652f\u6301SSL\/TLS\u52a0\u5bc6\u3001\u4f7f\u7528\u7b2c\u4e09\u65b9\u5e93<code>pysftp<\/code>\u3001<code>pycurl<\/code>\u548c<code>aioftp<\/code>\u7b49\u3002\u6bcf\u79cd\u65b9\u6cd5\u90fd\u6709\u5176\u4f18\u7f3a\u70b9\uff0c\u53ef\u4ee5\u6839\u636e\u5177\u4f53\u9700\u6c42\u9009\u62e9\u5408\u9002\u7684\u5b9e\u73b0\u65b9\u5f0f\u3002\u65e0\u8bba\u9009\u62e9\u54ea\u79cd\u65b9\u5f0f\uff0c\u90fd\u9700\u8981\u786e\u4fdd\u4f20\u8f93\u8fc7\u7a0b\u4e2d\u6570\u636e\u7684\u5b89\u5168\u6027\u548c\u5b8c\u6574\u6027\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u4f7f\u7528FTP\u8fdb\u884c\u5b89\u5168\u8fde\u63a5\uff1f<\/strong><br \/>\u8981\u5728Python\u4e2d\u5b9e\u73b0FTP\u7684\u5b89\u5168\u8fde\u63a5\uff0c\u901a\u5e38\u4f7f\u7528<code>ftplib<\/code>\u5e93\u5e76\u7ed3\u5408SSL\/TLS\u534f\u8bae\u3002\u53ef\u4ee5\u901a\u8fc7<code>ftplib.FTP_TLS<\/code>\u7c7b\u5b9e\u73b0\u5b89\u5168\u7684FTP\u4f20\u8f93\u3002\u8fd9\u79cd\u65b9\u5f0f\u53ef\u4ee5\u786e\u4fdd\u6570\u636e\u5728\u4f20\u8f93\u8fc7\u7a0b\u4e2d\u4e0d\u88ab\u7a83\u542c\u6216\u7be1\u6539\u3002\u4f60\u53ef\u4ee5\u4f7f\u7528<code>auth()<\/code>\u65b9\u6cd5\u542f\u52a8\u5b89\u5168\u8fde\u63a5\uff0c\u7136\u540e\u8fdb\u884c\u6587\u4ef6\u7684\u4e0a\u4f20\u548c\u4e0b\u8f7d\u3002<\/p>\n<p><strong>\u662f\u5426\u9700\u8981\u5b89\u88c5\u989d\u5916\u7684\u5e93\u6765\u5b9e\u73b0FTP\u52a0\u5bc6\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u6807\u51c6\u5e93<code>ftplib<\/code>\u5df2\u7ecf\u5305\u542b\u4e86\u652f\u6301FTP\u7684\u57fa\u672c\u529f\u80fd\u3002\u5982\u679c\u4f60\u9700\u8981\u5b9e\u73b0\u52a0\u5bc6\u4f20\u8f93\uff0c\u53ea\u9700\u4f7f\u7528<code>ftplib.FTP_TLS<\/code>\u7c7b\uff0c\u901a\u5e38\u4e0d\u9700\u8981\u5b89\u88c5\u989d\u5916\u7684\u5e93\u3002\u7136\u800c\uff0c\u786e\u4fdd\u4f60\u7684Python\u73af\u5883\u4e2d\u652f\u6301SSL\/TLS\u662f\u5fc5\u8981\u7684\uff0c\u56e0\u6b64\u53ef\u4ee5\u5728\u9700\u8981\u7684\u60c5\u51b5\u4e0b\u5b89\u88c5<code>pyOpenSSL<\/code>\u7b49\u5e93\u6765\u52a0\u5f3a\u5b89\u5168\u6027\u3002<\/p>\n<p><strong>\u5982\u4f55\u68c0\u67e5FTP\u670d\u52a1\u5668\u662f\u5426\u652f\u6301\u52a0\u5bc6\u4f20\u8f93\uff1f<\/strong><br \/>\u8981\u9a8c\u8bc1FTP\u670d\u52a1\u5668\u662f\u5426\u652f\u6301\u52a0\u5bc6\u4f20\u8f93\uff0c\u53ef\u4ee5\u67e5\u770b\u670d\u52a1\u5668\u7684\u6587\u6863\u6216\u914d\u7f6e\u6587\u4ef6\uff0c\u901a\u5e38\u4f1a\u6807\u660e\u652f\u6301\u7684\u534f\u8bae\u7c7b\u578b\u3002\u5982\u679c\u53ef\u80fd\uff0c\u4f7f\u7528\u547d\u4ee4\u884c\u5de5\u5177\u5982<code>ftp<\/code>\u6216<code>lftp<\/code>\u5c1d\u8bd5\u8fde\u63a5\uff0c\u5e76\u67e5\u770b\u670d\u52a1\u5668\u7684\u54cd\u5e94\u3002\u5982\u679c\u4f7f\u7528Python\uff0c\u5c1d\u8bd5\u4f7f\u7528<code>ftplib.FTP_TLS<\/code>\u8fde\u63a5\u65f6\uff0c\u5982\u679c\u8fde\u63a5\u5931\u8d25\u6216\u629b\u51fa\u5f02\u5e38\uff0c\u8bf4\u660e\u8be5\u670d\u52a1\u5668\u53ef\u80fd\u4e0d\u652f\u6301\u52a0\u5bc6\u4f20\u8f93\u3002<\/p>\n<p><strong>\u5728FTP\u52a0\u5bc6\u4f20\u8f93\u4e2d\uff0c\u5982\u4f55\u5904\u7406\u6587\u4ef6\u4f20\u8f93\u7684\u9519\u8bef\u548c\u5f02\u5e38\uff1f<\/strong><br \/>\u5728\u4f7f\u7528Python\u8fdb\u884cFTP\u52a0\u5bc6\u4f20\u8f93\u65f6\uff0c\u5efa\u8bae\u4f7f\u7528<code>try<\/code>\u548c<code>except<\/code>\u5757\u6765\u6355\u83b7\u53ef\u80fd\u53d1\u751f\u7684\u5f02\u5e38\uff0c\u4f8b\u5982\u8fde\u63a5\u5931\u8d25\u6216\u6587\u4ef6\u672a\u627e\u5230\u7b49\u3002\u8fd9\u6837\u53ef\u4ee5\u786e\u4fdd\u5728\u51fa\u73b0\u95ee\u9898\u65f6\uff0c\u80fd\u591f\u4f18\u96c5\u5730\u5904\u7406\u9519\u8bef\u5e76\u8f93\u51fa\u76f8\u5173\u4fe1\u606f\u3002\u5efa\u8bae\u8bb0\u5f55\u9519\u8bef\u65e5\u5fd7\uff0c\u4ee5\u4fbf\u540e\u7eed\u5206\u6790\u548c\u4fee\u590d\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"Python\u5b9e\u73b0FTP\u52a0\u5bc6\u4f20\u8f93\u7684\u65b9\u6cd5\u5305\u62ec\u4f7f\u7528ftplib\u6a21\u5757\u3001\u4f7f\u7528\u7b2c\u4e09\u65b9\u5e93\u5982paramiko\u3001\u914d\u7f6eSSL\/TL [&hellip;]","protected":false},"author":3,"featured_media":1032209,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[37],"tags":[],"acf":[],"_links":{"self":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1032200"}],"collection":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/comments?post=1032200"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1032200\/revisions"}],"predecessor-version":[{"id":1032213,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1032200\/revisions\/1032213"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1032209"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1032200"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1032200"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1032200"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}