{"id":2820,"date":"2025-03-16T12:14:50","date_gmt":"2025-03-16T04:14:50","guid":{"rendered":"https:\/\/www.laixuexila.com\/?p=2820"},"modified":"2025-03-16T12:14:50","modified_gmt":"2025-03-16T04:14:50","slug":"python3-%e7%bd%91%e7%bb%9c%e7%bc%96%e7%a8%8b4%ef%bc%9assh-%e4%b8%8e-ftp-%e7%bd%91%e7%bb%9c%e7%bc%96%e7%a8%8b","status":"publish","type":"post","link":"https:\/\/www.laixuexila.com\/index.php\/2025\/03\/16\/python3-%e7%bd%91%e7%bb%9c%e7%bc%96%e7%a8%8b4%ef%bc%9assh-%e4%b8%8e-ftp-%e7%bd%91%e7%bb%9c%e7%bc%96%e7%a8%8b\/","title":{"rendered":"Python3 \u7f51\u7edc\u7f16\u7a0b4\uff1aSSH \u4e0e FTP \u7f51\u7edc\u7f16\u7a0b"},"content":{"rendered":"\n<p>\u5728 Python \u4e2d\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528 <strong><code>paramiko<\/code><\/strong> \u8fdb\u884c SSH \u8fdc\u7a0b\u63a7\u5236\uff0c\u4f7f\u7528 <strong><code>ftplib<\/code><\/strong> \u8fdb\u884c FTP \u6587\u4ef6\u4f20\u8f93\u3002<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>4.1 \u4f7f\u7528 <code>paramiko<\/code> \u8fdb\u884c SSH \u8fdc\u7a0b\u64cd\u4f5c<\/strong><\/h2>\n\n\n\n<p><code>paramiko<\/code> \u662f\u4e00\u4e2a\u7528\u4e8e SSH \u8fde\u63a5\u7684 Python \u5e93\uff0c\u9002\u7528\u4e8e\u8fdc\u7a0b\u670d\u52a1\u5668\u7ba1\u7406\u3001\u6267\u884c\u547d\u4ee4\u548c\u6587\u4ef6\u4f20\u8f93\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>4.1.1 \u901a\u8fc7 SSH \u8fde\u63a5\u8fdc\u7a0b\u670d\u52a1\u5668\u5e76\u6267\u884c\u547d\u4ee4<\/strong><\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>import paramiko\n\nhost = \"192.168.1.100\"\nport = 22\nusername = \"user\"\npassword = \"password\"\n\n# \u521b\u5efa SSH \u5ba2\u6237\u7aef\nclient = paramiko.SSHClient()\nclient.set_missing_host_key_policy(paramiko.AutoAddPolicy())\n\ntry:\n    client.connect(host, port, username, password)\n    stdin, stdout, stderr = client.exec_command(\"ls -l\")\n    print(stdout.read().decode())  # \u8bfb\u53d6\u5e76\u6253\u5370\u547d\u4ee4\u8f93\u51fa\nfinally:\n    client.close()<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>4.1.2 \u901a\u8fc7 SSH \u4f20\u8f93\u6587\u4ef6\uff08SFTP\uff09<\/strong><\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>import paramiko\n\nhost = \"192.168.1.100\"\nport = 22\nusername = \"user\"\npassword = \"password\"\n\ntransport = paramiko.Transport((host, port))\ntransport.connect(username=username, password=password)\n\nsftp = paramiko.SFTPClient.from_transport(transport)\nsftp.put(\"local_file.txt\", \"\/remote\/path\/file.txt\")  # \u4e0a\u4f20\u6587\u4ef6\nsftp.get(\"\/remote\/path\/file.txt\", \"downloaded_file.txt\")  # \u4e0b\u8f7d\u6587\u4ef6\n\nsftp.close()\ntransport.close()<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>4.2 \u4f7f\u7528 <code>ftplib<\/code> \u8fdb\u884c FTP \u6587\u4ef6\u4f20\u8f93<\/strong><\/h2>\n\n\n\n<p><code>ftplib<\/code> \u662f Python \u5185\u7f6e\u7684 FTP \u4f20\u8f93\u5e93\uff0c\u53ef\u7528\u4e8e\u4e0a\u4f20\u3001\u4e0b\u8f7d\u548c\u7ba1\u7406\u6587\u4ef6\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>4.2.1 \u8fde\u63a5 FTP \u670d\u52a1\u5668<\/strong><\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>from ftplib import FTP\n\nftp = FTP(\"ftp.example.com\")\nftp.login(\"username\", \"password\")\n\nprint(ftp.getwelcome())  # \u8f93\u51fa FTP \u670d\u52a1\u5668\u6b22\u8fce\u4fe1\u606f\nftp.quit()<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>4.2.2 \u83b7\u53d6\u76ee\u5f55\u5217\u8868<\/strong><\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>ftp = FTP(\"ftp.example.com\")\nftp.login(\"username\", \"password\")\n\nftp.cwd(\"\/\")  # \u8fdb\u5165\u6839\u76ee\u5f55\nfiles = ftp.nlst()  # \u83b7\u53d6\u6587\u4ef6\u5217\u8868\nprint(\"\u670d\u52a1\u5668\u6587\u4ef6\u5217\u8868:\", files)\n\nftp.quit()<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>4.2.3 \u4e0a\u4f20\u6587\u4ef6<\/strong><\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>with open(\"local_file.txt\", \"rb\") as file:\n    ftp.storbinary(\"STOR remote_file.txt\", file)<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>4.2.4 \u4e0b\u8f7d\u6587\u4ef6<\/strong><\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>with open(\"downloaded_file.txt\", \"wb\") as file:\n    ftp.retrbinary(\"RETR remote_file.txt\", file.write)<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<p><strong>\u4e0b\u7bc7\u6587\u7ae0\u6211\u4eec\u5c06\u4ecb\u7ecd <code>socketserver<\/code> \u521b\u5efa\u81ea\u5b9a\u4e49\u670d\u52a1\u5668\uff0c\u5e76\u6df1\u5165\u89e3\u6790 <code>scapy<\/code> \u8fdb\u884c\u7f51\u7edc\u6570\u636e\u5305\u5206\u6790\uff01<\/strong> <\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5728 Python \u4e2d\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528 paramiko \u8fdb\u884c SSH \u8fdc\u7a0b\u63a7\u5236\uff0c\u4f7f\u7528 ftplib \u8fdb\u884c FTP [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":2821,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[79],"tags":[],"class_list":["post-2820","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-python-3-"],"_links":{"self":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts\/2820","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/comments?post=2820"}],"version-history":[{"count":1,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts\/2820\/revisions"}],"predecessor-version":[{"id":2823,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts\/2820\/revisions\/2823"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/media\/2821"}],"wp:attachment":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/media?parent=2820"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/categories?post=2820"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/tags?post=2820"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}