{"id":234,"date":"2024-01-10T20:57:06","date_gmt":"2024-01-10T12:57:06","guid":{"rendered":"https:\/\/wangqianming.top\/?p=234"},"modified":"2024-01-10T20:57:06","modified_gmt":"2024-01-10T12:57:06","slug":"python%e8%b0%83%e7%94%a8%e5%91%bd%e4%bb%a4%e8%a1%8c","status":"publish","type":"post","link":"https:\/\/wangqianming.top\/index.php\/2024\/01\/10\/python%e8%b0%83%e7%94%a8%e5%91%bd%e4%bb%a4%e8%a1%8c\/","title":{"rendered":"python\u8c03\u7528\u547d\u4ee4\u884c"},"content":{"rendered":"<h1>\u4f7f\u7528python\u6765\u8c03\u7528\u547d\u4ee4\u884c<\/h1>\n<p>python\u672c\u6765\u4e5f\u5c31\u662f\u4e00\u4e2a\u811a\u672c\u8bed\u8a00\uff0c\u548cwindows\u4e2d\u7684PowerShell\uff0c\u4ee5\u53caUnix\/Linux\u4e2d\u7684Shell\u672c\u8d28\u4e0a\u662f\u4e00\u7c7b\u8bed\u8a00\uff0c\u6240\u4ee5\u4f7f\u7528python\u8c03\u7528windows\u4e2d\u7684\u547d\u4ee4\u884c\u5c31\u6bd4\u8f83\u65b9\u4fbf\u3002<\/p>\n<p>\u4e0b\u9762\u7684\u4ee3\u7801\u4f7f\u7528python\u6267\u884cwindows\u4e2d\u7684\u547d\u4ee4\u884c\u7684\u4e00\u4e9b\u4f8b\u5b50\uff0c\u6bd4\u5982\u79fb\u52a8\u5230\u67d0\u4e2a\u76ee\u5f55\uff0c\u7136\u540e\u6267\u884c<code>adb connect 192.168.168.110<\/code>\u7b49\u547d\u4ee4<\/p>\n<p>\u5728Python\u4e2d\u6267\u884cWindows\u547d\u4ee4\u884c\u64cd\u4f5c\u65f6\uff0c\u60a8\u53ef\u4ee5\u4f7f\u7528<code>subprocess<\/code>\u6a21\u5757\u3002\u4e0b\u9762\u6211\u5c06\u63d0\u4f9b\u4e00\u4e2a\u4f8b\u5b50\uff0c\u5c55\u793a\u5982\u4f55\u4f7f\u7528Python\u5148\u5207\u6362\u5230\u6307\u5b9a\u76ee\u5f55\uff0c\u7136\u540e\u6267\u884c<code>adb connect 192.168.168.110<\/code>\u7b49\u547d\u4ee4\u3002<\/p>\n<p>\u7531\u4e8e<code>subprocess<\/code>\u65e0\u6cd5\u76f4\u63a5\u66f4\u6539Python\u811a\u672c\u7684\u5f53\u524d\u5de5\u4f5c\u76ee\u5f55\uff0c\u60a8\u9700\u8981\u5148\u4f7f\u7528Python\u7684<code>os.chdir()<\/code>\u65b9\u6cd5\u66f4\u6539\u5de5\u4f5c\u76ee\u5f55\uff0c\u7136\u540e\u518d\u6267\u884c<code>adb<\/code>\u547d\u4ee4\u3002<\/p>\n<p>\u8fd9\u91cc\u662f\u4e00\u4e2a\u57fa\u672c\u7684\u4ee3\u7801\u793a\u4f8b\uff1a<\/p>\n<pre><code class=\"language-python\">import subprocess\nimport os\n\n# \u66f4\u6539\u5f53\u524d\u5de5\u4f5c\u76ee\u5f55\ndesired_path = &quot;C:\\\\path\\\\to\\\\your\\\\directory&quot;\nos.chdir(desired_path)\n\ndef run_command(command):\n    &quot;&quot;&quot;\u6267\u884c\u7ed9\u5b9a\u7684\u547d\u4ee4\u5e76\u8fd4\u56de\u8f93\u51fa&quot;&quot;&quot;\n    process = subprocess.run(command, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True)\n    return process.stdout, process.stderr\n\n# \u8fde\u63a5\u5230ADB\u8bbe\u5907\nconnect_output, connect_error = run_command(&quot;adb connect 192.168.168.110&quot;)\nprint(&quot;Connect Output:&quot;, connect_output)\nprint(&quot;Connect Error:&quot;, connect_error)\n\n# \u5217\u51fa\u8fde\u63a5\u7684ADB\u8bbe\u5907\ndevices_output, devices_error = run_command(&quot;adb devices&quot;)\nprint(&quot;Devices Output:&quot;, devices_output)\nprint(&quot;Devices Error:&quot;, devices_error)\n\n# \u5728\u7279\u5b9a\u4f4d\u7f6e\u6267\u884c\u5c4f\u5e55\u70b9\u51fb\ntap_output, tap_error = run_command(&quot;adb shell input tap 1036 300&quot;)\nprint(&quot;Tap Output:&quot;, tap_output)\nprint(&quot;Tap Error:&quot;, tap_error)<\/code><\/pre>\n<p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff1a<\/p>\n<ol>\n<li>\u4f7f\u7528<code>os.chdir()<\/code>\u66f4\u6539\u5f53\u524d\u76ee\u5f55\u5230\u60a8\u5e0c\u671b\u7684\u76ee\u5f55\u3002<\/li>\n<li>\u4f7f\u7528<code>subprocess.run()<\/code>\u6267\u884c<code>adb connect<\/code>\u547d\u4ee4\u3002<\/li>\n<li>\u901a\u8fc7\u8bbe\u7f6e<code>stdout<\/code>\u548c<code>stderr<\/code>\u53c2\u6570\u6765\u6355\u83b7\u547d\u4ee4\u7684\u6807\u51c6\u8f93\u51fa\u548c\u9519\u8bef\u8f93\u51fa\u3002<\/li>\n<li>\u8bbe\u7f6e<code>shell=True<\/code>\u4ee5\u5728shell\u73af\u5883\u4e2d\u8fd0\u884c\u547d\u4ee4\u3002\u8fd9\u5bf9\u4e8e\u67d0\u4e9b\u547d\u4ee4\u662f\u5fc5\u8981\u7684\uff0c\u4f46\u8bf7\u6ce8\u610f\uff0c\u8fd9\u4e5f\u53ef\u80fd\u5e26\u6765\u5b89\u5168\u98ce\u9669\uff0c\u7279\u522b\u662f\u5728\u6267\u884c\u4e0d\u53d7\u4fe1\u4efb\u7684\u8f93\u5165\u65f6\u3002<\/li>\n<\/ol>\n<p>\u8bf7\u786e\u4fdd\u60a8\u7684\u8def\u5f84\u548c\u547d\u4ee4\u6839\u636e\u60a8\u7684\u5b9e\u9645\u9700\u6c42\u8fdb\u884c\u8c03\u6574\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u4f7f\u7528python\u6765\u8c03\u7528\u547d\u4ee4\u884c python\u672c\u6765\u4e5f\u5c31\u662f\u4e00\u4e2a\u811a\u672c\u8bed\u8a00\uff0c\u548cwindows\u4e2d\u7684PowerShell\uff0c\u4ee5\u53caUnix\/Linu &#8230;<\/p>","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[24],"class_list":["post-234","post","type-post","status-publish","format-standard","hentry","category-uncategorized","tag-python"],"_links":{"self":[{"href":"https:\/\/wangqianming.top\/index.php\/wp-json\/wp\/v2\/posts\/234","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/wangqianming.top\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/wangqianming.top\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/wangqianming.top\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/wangqianming.top\/index.php\/wp-json\/wp\/v2\/comments?post=234"}],"version-history":[{"count":1,"href":"https:\/\/wangqianming.top\/index.php\/wp-json\/wp\/v2\/posts\/234\/revisions"}],"predecessor-version":[{"id":235,"href":"https:\/\/wangqianming.top\/index.php\/wp-json\/wp\/v2\/posts\/234\/revisions\/235"}],"wp:attachment":[{"href":"https:\/\/wangqianming.top\/index.php\/wp-json\/wp\/v2\/media?parent=234"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/wangqianming.top\/index.php\/wp-json\/wp\/v2\/categories?post=234"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/wangqianming.top\/index.php\/wp-json\/wp\/v2\/tags?post=234"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}