{"id":1016398,"date":"2024-12-27T12:16:07","date_gmt":"2024-12-27T04:16:07","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1016398.html"},"modified":"2024-12-27T12:16:09","modified_gmt":"2024-12-27T04:16:09","slug":"%e5%a6%82%e4%bd%95%e8%8e%b7%e5%8f%96%e8%bf%9c%e7%a8%8bpython%e6%96%87%e4%bb%b6","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1016398.html","title":{"rendered":"\u5982\u4f55\u83b7\u53d6\u8fdc\u7a0bpython\u6587\u4ef6"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25095940\/ee9b20fc-2496-4cf5-b500-ebaed1e9b625.webp\" alt=\"\u5982\u4f55\u83b7\u53d6\u8fdc\u7a0bpython\u6587\u4ef6\" \/><\/p>\n<p><p> \u83b7\u53d6\u8fdc\u7a0bPython\u6587\u4ef6\u7684\u65b9\u6cd5\u6709\u591a\u79cd\uff0c\u4e3b\u8981\u5305\u62ec<strong>\u4f7f\u7528HTTP\u8bf7\u6c42\u4e0b\u8f7d\u3001\u901a\u8fc7FTP\u83b7\u53d6\u3001\u4f7f\u7528\u7248\u672c\u63a7\u5236\u7cfb\u7edf\u514b\u9686\u3001\u5229\u7528\u4e91\u5b58\u50a8\u670d\u52a1<\/strong>\u7b49\u3002\u5176\u4e2d\uff0c\u4f7f\u7528HTTP\u8bf7\u6c42\u4e0b\u8f7d\u662f\u6700\u5e38\u89c1\u7684\u65b9\u6cd5\u4e4b\u4e00\u3002\u901a\u8fc7Python\u4e2d\u7684<code>requests<\/code>\u5e93\uff0c\u53ef\u4ee5\u8f7b\u677e\u5730\u4ece\u7f51\u7edc\u4e0a\u4e0b\u8f7dPython\u6587\u4ef6\u3002\u4e0b\u9762\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u8fd9\u79cd\u65b9\u6cd5\uff0c\u5e76\u63a2\u8ba8\u5176\u4ed6\u51e0\u79cd\u5e38\u7528\u65b9\u5f0f\u3002<\/p>\n<\/p>\n<p><p>\u4e00\u3001\u4f7f\u7528HTTP\u8bf7\u6c42\u4e0b\u8f7d<\/p>\n<\/p>\n<p><p>\u4f7f\u7528HTTP\u8bf7\u6c42\u4e0b\u8f7d\u8fdc\u7a0bPython\u6587\u4ef6\u662f\u4e00\u79cd\u975e\u5e38\u666e\u904d\u7684\u65b9\u6cd5\u3002Python\u7684<code>requests<\/code>\u5e93\u63d0\u4f9b\u4e86\u7b80\u6d01\u7684API\u6765\u5904\u7406HTTP\u8bf7\u6c42\u3002<\/p>\n<\/p>\n<ol>\n<li><strong>\u5b89\u88c5<code>requests<\/code>\u5e93<\/strong><\/li>\n<\/ol>\n<p><p>\u5728\u5f00\u59cb\u4e4b\u524d\uff0c\u786e\u4fdd\u5df2\u5b89\u88c5<code>requests<\/code>\u5e93\u3002\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u547d\u4ee4\u5b89\u88c5\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pip install requests<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ol start=\"2\">\n<li><strong>\u4f7f\u7528<code>requests<\/code>\u5e93\u4e0b\u8f7d\u6587\u4ef6<\/strong><\/li>\n<\/ol>\n<p><p>\u4f7f\u7528<code>requests.get()<\/code>\u65b9\u6cd5\u53d1\u9001HTTP GET\u8bf7\u6c42\uff0c\u7136\u540e\u5c06\u54cd\u5e94\u5185\u5bb9\u5199\u5165\u672c\u5730\u6587\u4ef6\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u7b80\u5355\u7684\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import requests<\/p>\n<p>def download_file(url, local_filename):<\/p>\n<p>    with requests.get(url, stream=True) as r:<\/p>\n<p>        r.r<a href=\"https:\/\/docs.pingcode.com\/blog\/59162.html\" target=\"_blank\">AI<\/a>se_for_status()<\/p>\n<p>        with open(local_filename, &#39;wb&#39;) as f:<\/p>\n<p>            for chunk in r.iter_content(chunk_size=8192):<\/p>\n<p>                f.write(chunk)<\/p>\n<p>    return local_filename<\/p>\n<h2><strong>\u793a\u4f8b\u7528\u6cd5<\/strong><\/h2>\n<p>url = &#39;https:\/\/example.com\/path\/to\/your\/file.py&#39;<\/p>\n<p>download_file(url, &#39;downloaded_file.py&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>\u5728\u8fd9\u4e2a\u8fc7\u7a0b\u4e2d\uff0c\u9996\u5148\u6211\u4eec\u4f7f\u7528<code>requests.get()<\/code>\u65b9\u6cd5\u53d1\u9001\u8bf7\u6c42\uff0c\u7136\u540e\u901a\u8fc7<code>r.iter_content()<\/code>\u65b9\u6cd5\u4ee5\u6d41\u7684\u65b9\u5f0f\u4e0b\u8f7d\u6587\u4ef6\u5185\u5bb9\uff0c\u6700\u540e\u5c06\u5176\u5199\u5165\u672c\u5730\u6587\u4ef6\u3002<\/strong><\/p>\n<\/p>\n<p><p>\u4e8c\u3001\u901a\u8fc7FTP\u83b7\u53d6<\/p>\n<\/p>\n<p><p>FTP\uff08\u6587\u4ef6\u4f20\u8f93\u534f\u8bae\uff09\u662f\u4e00\u79cd\u7528\u4e8e\u5728\u7f51\u7edc\u4e0a\u8fdb\u884c\u6587\u4ef6\u4f20\u8f93\u7684\u6807\u51c6\u534f\u8bae\u3002\u53ef\u4ee5\u4f7f\u7528Python\u7684<code>ftplib<\/code>\u5e93\u6765\u83b7\u53d6\u8fdc\u7a0bPython\u6587\u4ef6\u3002<\/p>\n<\/p>\n<ol>\n<li><strong>\u8fde\u63a5\u5230FTP\u670d\u52a1\u5668<\/strong><\/li>\n<\/ol>\n<p><p>\u9996\u5148\uff0c\u4f7f\u7528<code>ftplib.FTP<\/code>\u7c7b\u8fde\u63a5\u5230FTP\u670d\u52a1\u5668\uff0c\u5e76\u767b\u5f55\u5230\u670d\u52a1\u5668\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from ftplib import FTP<\/p>\n<p>ftp = FTP(&#39;ftp.example.com&#39;)<\/p>\n<p>ftp.login(user=&#39;username&#39;, passwd=&#39;password&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ol start=\"2\">\n<li><strong>\u4e0b\u8f7d\u6587\u4ef6<\/strong><\/li>\n<\/ol>\n<p><p>\u8fde\u63a5\u6210\u529f\u540e\uff0c\u53ef\u4ee5\u4f7f\u7528<code>retrbinary()<\/code>\u65b9\u6cd5\u4e0b\u8f7d\u6587\u4ef6\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def download_ftp_file(ftp, filepath, local_filename):<\/p>\n<p>    with open(local_filename, &#39;wb&#39;) as f:<\/p>\n<p>        ftp.retrbinary(f&#39;RETR {filepath}&#39;, f.write)<\/p>\n<h2><strong>\u793a\u4f8b\u7528\u6cd5<\/strong><\/h2>\n<p>download_ftp_file(ftp, &#39;\/path\/to\/your\/file.py&#39;, &#39;downloaded_file.py&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>\u5728\u8fd9\u4e2a\u8fc7\u7a0b\u4e2d\uff0c\u4f7f\u7528<code>ftp.retrbinary()<\/code>\u65b9\u6cd5\u53ef\u4ee5\u5b9e\u73b0\u4ee5\u4e8c\u8fdb\u5236\u6a21\u5f0f\u4e0b\u8f7d\u6587\u4ef6\uff0c\u786e\u4fdd\u6587\u4ef6\u5185\u5bb9\u4e0d\u4f1a\u56e0\u6587\u672c\u7f16\u7801\u95ee\u9898\u800c\u635f\u574f\u3002<\/strong><\/p>\n<\/p>\n<p><p>\u4e09\u3001\u4f7f\u7528\u7248\u672c\u63a7\u5236\u7cfb\u7edf\u514b\u9686<\/p>\n<\/p>\n<p><p>\u7248\u672c\u63a7\u5236\u7cfb\u7edf\uff08\u5982Git\uff09\u53ef\u4ee5\u65b9\u4fbf\u5730\u7ba1\u7406\u548c\u83b7\u53d6\u8fdc\u7a0b\u4ee3\u7801\u6587\u4ef6\u3002\u5229\u7528Git\uff0c\u53ef\u4ee5\u8f7b\u677e\u514b\u9686\u8fdc\u7a0b\u5b58\u50a8\u5e93\u5e76\u83b7\u53d6\u6240\u9700\u7684Python\u6587\u4ef6\u3002<\/p>\n<\/p>\n<ol>\n<li><strong>\u5b89\u88c5Git<\/strong><\/li>\n<\/ol>\n<p><p>\u786e\u4fdd\u672c\u5730\u5df2\u5b89\u88c5Git\u3002\u53ef\u4ee5\u4ece<a href=\"https:\/\/git-scm.com\/\">Git\u5b98\u7f51<\/a>\u4e0b\u8f7d\u5e76\u5b89\u88c5\u3002<\/p>\n<\/p>\n<ol start=\"2\">\n<li><strong>\u514b\u9686\u5b58\u50a8\u5e93<\/strong><\/li>\n<\/ol>\n<p><p>\u4f7f\u7528<code>git clone<\/code>\u547d\u4ee4\u514b\u9686\u8fdc\u7a0b\u5b58\u50a8\u5e93\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">git clone https:\/\/github.com\/username\/repository.git<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ol start=\"3\">\n<li><strong>\u5bfc\u822a\u5230\u6587\u4ef6<\/strong><\/li>\n<\/ol>\n<p><p>\u514b\u9686\u5b8c\u6210\u540e\uff0c\u5bfc\u822a\u5230\u5b58\u50a8\u5e93\u76ee\u5f55\u5e76\u627e\u5230\u6240\u9700\u7684Python\u6587\u4ef6\u3002<\/p>\n<\/p>\n<p><p><strong>\u4f7f\u7528Git\u4e0d\u4ec5\u53ef\u4ee5\u83b7\u53d6\u8fdc\u7a0bPython\u6587\u4ef6\uff0c\u8fd8\u53ef\u4ee5\u8ddf\u8e2a\u6587\u4ef6\u7684\u5386\u53f2\u7248\u672c\uff0c\u4fbf\u4e8e\u534f\u4f5c\u5f00\u53d1\u3002<\/strong><\/p>\n<\/p>\n<p><p>\u56db\u3001\u5229\u7528\u4e91\u5b58\u50a8\u670d\u52a1<\/p>\n<\/p>\n<p><p>\u4e91\u5b58\u50a8\u670d\u52a1\uff08\u5982Google Drive\u3001Dropbox\uff09\u63d0\u4f9b\u4e86\u4fbf\u6377\u7684\u6587\u4ef6\u5171\u4eab\u548c\u4e0b\u8f7d\u529f\u80fd\u3002\u53ef\u4ee5\u901a\u8fc7\u5176API\u83b7\u53d6\u8fdc\u7a0bPython\u6587\u4ef6\u3002<\/p>\n<\/p>\n<ol>\n<li><strong>\u4f7f\u7528Google Drive API<\/strong><\/li>\n<\/ol>\n<p><p>\u9700\u8981\u5728Google Cloud Platform\u4e0a\u542f\u7528Drive API\uff0c\u5e76\u83b7\u53d6OAuth2\u51ed\u636e\u3002<\/p>\n<\/p>\n<ol start=\"2\">\n<li><strong>\u5b89\u88c5<code>pydrive<\/code>\u5e93<\/strong><\/li>\n<\/ol>\n<p><p>\u4f7f\u7528\u4ee5\u4e0b\u547d\u4ee4\u5b89\u88c5<code>pydrive<\/code>\u5e93\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pip install pydrive<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ol start=\"3\">\n<li><strong>\u4e0b\u8f7d\u6587\u4ef6<\/strong><\/li>\n<\/ol>\n<p><p>\u901a\u8fc7<code>pydrive<\/code>\u5e93\u4e0b\u8f7d\u6587\u4ef6\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from pydrive.auth import GoogleAuth<\/p>\n<p>from pydrive.drive import GoogleDrive<\/p>\n<p>gauth = GoogleAuth()<\/p>\n<p>gauth.LocalWebserverAuth()<\/p>\n<p>drive = GoogleDrive(gauth)<\/p>\n<h2><strong>\u83b7\u53d6\u6587\u4ef6\u5217\u8868<\/strong><\/h2>\n<p>file_list = drive.ListFile({&#39;q&#39;: &quot;&#39;root&#39; in parents and trashed=false&quot;}).GetList()<\/p>\n<h2><strong>\u4e0b\u8f7d\u7279\u5b9a\u6587\u4ef6<\/strong><\/h2>\n<p>for file in file_list:<\/p>\n<p>    if file[&#39;title&#39;] == &#39;file.py&#39;:<\/p>\n<p>        file.GetContentFile(&#39;downloaded_file.py&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>\u8fd9\u79cd\u65b9\u6cd5\u9700\u8981\u4e8b\u5148\u914d\u7f6eGoogle Cloud Platform\u4e2d\u7684API\u6743\u9650\uff0c\u4f46\u80fd\u591f\u65b9\u4fbf\u5730\u8bbf\u95ee\u548c\u7ba1\u7406\u5b58\u50a8\u5728Google Drive\u4e2d\u7684\u6587\u4ef6\u3002<\/strong><\/p>\n<\/p>\n<p><p>\u603b\u7ed3\uff1a<\/p>\n<\/p>\n<p><p>\u83b7\u53d6\u8fdc\u7a0bPython\u6587\u4ef6\u7684\u65b9\u6cd5\u6709\u591a\u79cd\uff0c\u9009\u62e9\u9002\u5408\u7684\u65b9\u6cd5\u53ef\u4ee5\u63d0\u9ad8\u6548\u7387\uff0c\u8282\u7701\u65f6\u95f4\u3002<strong>\u4f7f\u7528HTTP\u8bf7\u6c42\u4e0b\u8f7d\u662f\u6700\u5e38\u89c1\u7684\u65b9\u6cd5\uff0c\u7b80\u5355\u6613\u7528\uff0c\u9002\u5408\u5927\u591a\u6570\u573a\u666f\uff1b\u901a\u8fc7FTP\u83b7\u53d6\u9002\u7528\u4e8e\u9700\u8981\u9ad8\u6548\u4f20\u8f93\u5927\u91cf\u6587\u4ef6\u7684\u573a\u666f\uff1b\u4f7f\u7528\u7248\u672c\u63a7\u5236\u7cfb\u7edf\u514b\u9686\u9002\u5408\u56e2\u961f\u534f\u4f5c\u5f00\u53d1\uff1b\u5229\u7528\u4e91\u5b58\u50a8\u670d\u52a1\u5219\u9002\u5408\u9700\u8981\u5171\u4eab\u548c\u5907\u4efd\u6587\u4ef6\u7684\u573a\u666f<\/strong>\u3002\u6839\u636e\u5177\u4f53\u9700\u6c42\u548c\u73af\u5883\uff0c\u9009\u62e9\u5408\u9002\u7684\u65b9\u6cd5\u53ef\u4ee5\u66f4\u597d\u5730\u5b8c\u6210\u4efb\u52a1\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5b89\u5168\u5730\u4e0b\u8f7d\u8fdc\u7a0bPython\u6587\u4ef6\uff1f<\/strong><br \/>\u4e0b\u8f7d\u8fdc\u7a0bPython\u6587\u4ef6\u65f6\uff0c\u786e\u4fdd\u4f7f\u7528\u5b89\u5168\u7684\u8fde\u63a5\uff08\u5982HTTPS\uff09\u548c\u4fe1\u4efb\u7684\u6765\u6e90\u3002\u53ef\u4ee5\u4f7f\u7528<code>requests<\/code>\u5e93\u6216<code>wget<\/code>\u547d\u4ee4\u884c\u5de5\u5177\u3002\u4e0b\u8f7d\u540e\uff0c\u68c0\u67e5\u6587\u4ef6\u5185\u5bb9\uff0c\u786e\u4fdd\u6ca1\u6709\u6076\u610f\u4ee3\u7801\u3002<\/p>\n<p><strong>\u6709\u54ea\u4e9b\u5e38\u7528\u7684\u65b9\u6cd5\u53ef\u4ee5\u83b7\u53d6\u8fdc\u7a0bPython\u6587\u4ef6\uff1f<\/strong><br \/>\u83b7\u53d6\u8fdc\u7a0bPython\u6587\u4ef6\u7684\u65b9\u6cd5\u6709\u591a\u79cd\uff0c\u5305\u62ec\u4f7f\u7528<code>curl<\/code>\u547d\u4ee4\u3001<code>requests<\/code>\u5e93\u3001FTP\u6216SFTP\u534f\u8bae\u7b49\u3002\u9009\u62e9\u5408\u9002\u7684\u65b9\u6cd5\u53d6\u51b3\u4e8e\u6587\u4ef6\u7684\u5b58\u50a8\u4f4d\u7f6e\u548c\u8bbf\u95ee\u6743\u9650\u3002<\/p>\n<p><strong>\u5982\u4f55\u5728\u672c\u5730\u8fd0\u884c\u4e0b\u8f7d\u7684\u8fdc\u7a0bPython\u6587\u4ef6\uff1f<\/strong><br \/>\u4e0b\u8f7d\u8fdc\u7a0bPython\u6587\u4ef6\u540e\uff0c\u786e\u4fdd\u6240\u9700\u7684\u4f9d\u8d56\u9879\u5df2\u5b89\u88c5\u3002\u53ef\u4ee5\u901a\u8fc7<code>pip<\/code>\u5b89\u88c5\u7f3a\u5c11\u7684\u5e93\u3002\u4f7f\u7528\u547d\u4ee4\u884c\u8fdb\u5165\u6587\u4ef6\u6240\u5728\u76ee\u5f55\uff0c\u5e76\u8fd0\u884c<code>python \u6587\u4ef6\u540d.py<\/code>\u6765\u6267\u884c\u8be5\u811a\u672c\u3002<\/p>\n<p><strong>\u8fdc\u7a0bPython\u6587\u4ef6\u7684\u7248\u672c\u517c\u5bb9\u6027\u5982\u4f55\u5904\u7406\uff1f<\/strong><br \/>\u5728\u4e0b\u8f7d\u548c\u8fd0\u884c\u8fdc\u7a0bPython\u6587\u4ef6\u65f6\uff0c\u786e\u8ba4\u5176\u4e0e\u672c\u5730Python\u73af\u5883\u7684\u7248\u672c\u517c\u5bb9\u3002\u53ef\u4ee5\u67e5\u9605\u4ee3\u7801\u4e2d\u7684\u6587\u6863\u6216\u6ce8\u91ca\uff0c\u5fc5\u8981\u65f6\u8fdb\u884c\u4fee\u6539\u4ee5\u9002\u5e94\u672c\u5730\u73af\u5883\u3002\u4f7f\u7528<code>virtualenv<\/code>\u521b\u5efa\u9694\u79bb\u73af\u5883\u53ef\u4ee5\u66f4\u597d\u5730\u7ba1\u7406\u4e0d\u540c\u7248\u672c\u7684\u4f9d\u8d56\u9879\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u83b7\u53d6\u8fdc\u7a0bPython\u6587\u4ef6\u7684\u65b9\u6cd5\u6709\u591a\u79cd\uff0c\u4e3b\u8981\u5305\u62ec\u4f7f\u7528HTTP\u8bf7\u6c42\u4e0b\u8f7d\u3001\u901a\u8fc7FTP\u83b7\u53d6\u3001\u4f7f\u7528\u7248\u672c\u63a7\u5236\u7cfb\u7edf\u514b\u9686\u3001\u5229\u7528 [&hellip;]","protected":false},"author":3,"featured_media":1016403,"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\/1016398"}],"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=1016398"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1016398\/revisions"}],"predecessor-version":[{"id":1016404,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1016398\/revisions\/1016404"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1016403"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1016398"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1016398"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1016398"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}