{"id":1945,"date":"2024-05-11T21:14:43","date_gmt":"2024-05-11T13:14:43","guid":{"rendered":"http:\/\/blog.xtaa.cn\/?p=1945"},"modified":"2024-06-24T20:46:14","modified_gmt":"2024-06-24T12:46:14","slug":"%e4%b8%8b%e8%bd%bd%e7%94%a8%e6%9c%ba%e9%a1%b6%e7%9b%92","status":"publish","type":"post","link":"http:\/\/blog.xtaa.cn\/index.php\/2024\/05\/11\/%e4%b8%8b%e8%bd%bd%e7%94%a8%e6%9c%ba%e9%a1%b6%e7%9b%92\/","title":{"rendered":"\u4e0b\u8f7d\u7528\u673a\u9876\u76d2"},"content":{"rendered":"\n<p>\u5728\u673a\u9876\u76d2\u5237\u4e2ahi-nas\uff0c\u7136\u540e\u8fdb\u7ec8\u7aef\uff0c\u88c5python3,\u518d\u5f00\u673a\u5c31\u8fd0\u884cpy\u6587\u4ef6<\/p>\n\n\n\n<p><code>systemd<\/code>\u662f\u73b0\u4ee3Linux\u7cfb\u7edf\u4e2d\u7528\u4e8e\u521d\u59cb\u5316\u7cfb\u7edf\u7ec4\u4ef6\u548c\u7ba1\u7406\u7cfb\u7edf\u8fdb\u7a0b\u7684\u6807\u51c6\u3002\u4ee5\u4e0b\u662f\u521b\u5efa\u4e00\u4e2a<code>systemd<\/code>\u670d\u52a1\u7528\u4e8e\u5f00\u673a\u5c31\u542f\u52a8\u8fd9\u4e2a\u6587\u4ef6\uff0c\u57fa\u672c\u6b65\u9aa4\uff1a<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li>\u521b\u5efa\u4e00\u4e2a\u670d\u52a1\u6587\u4ef6\uff0c\u4f8b\u5982<code>\/etc\/systemd\/system\/myservice.service<\/code>\uff0c\u5185\u5bb9\u53ef\u80fd\u5982\u4e0b\uff1a<\/li>\n<\/ol>\n\n\n\n<pre class=\"wp-block-code\"><code>&#91;Unit]\nDescription=My Python Service\nAfter=network.target\n\n&#91;Service]\nUser=yourusername\nExecStart=\/usr\/bin\/python3 \/path\/to\/your_script.py\nRestart=always\nWorkingDirectory=\/path\/to\/your_script_directory\n\n&#91;Install]\nWantedBy=multi-user.target<\/code><\/pre>\n\n\n\n<p>\u8bf7\u5c06<code>yourusername<\/code>\u66ff\u6362\u4e3a\u4f60\u7684\u7528\u6237\u540d\uff0c<code>\/path\/to\/your_script.py<\/code>\u66ff\u6362\u4e3a\u4f60\u7684Python\u811a\u672c\u7684\u5b9e\u9645\u8def\u5f84\uff0c<code>\/path\/to\/your_script_directory<\/code>\u66ff\u6362\u4e3a\u4f60\u7684Python\u811a\u672c\u6240\u5728\u7684\u76ee\u5f55\u3002<\/p>\n\n\n\n<ol class=\"wp-block-list\" start=\"2\">\n<li>\u91cd\u65b0\u52a0\u8f7d<code>systemd<\/code>\u914d\u7f6e\u4ee5\u8bc6\u522b\u65b0\u670d\u52a1\uff1a<\/li>\n<\/ol>\n\n\n\n<pre class=\"wp-block-code\"><code>sudo systemctl daemon-reload<\/code><\/pre>\n\n\n\n<ol class=\"wp-block-list\" start=\"3\">\n<li>\u542f\u52a8\u670d\u52a1\uff1a<\/li>\n<\/ol>\n\n\n\n<pre class=\"wp-block-preformatted\">\u590d\u5236<code>sudo systemctl start myservice.service<\/code><\/pre>\n\n\n\n<ol class=\"wp-block-list\" start=\"4\">\n<li>\u4f7f\u670d\u52a1\u5728\u5f00\u673a\u65f6\u81ea\u52a8\u542f\u52a8\uff1a<\/li>\n<\/ol>\n\n\n\n<pre class=\"wp-block-code\"><code>sudo systemctl enable myservice.service<\/code><\/pre>\n\n\n\n<ol class=\"wp-block-list\" start=\"5\">\n<li>\u68c0\u67e5\u670d\u52a1\u72b6\u6001\uff1a<\/li>\n<\/ol>\n\n\n\n<pre class=\"wp-block-code\"><code>sudo systemctl status myservice.service<\/code><\/pre>\n\n\n\n<p><\/p>\n\n\n\n<p>\u4ee5\u4e0b\u662f\u7528\u5230\u7684py\u6587\u4ef6<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import requests\nimport time\nimport datetime\nfrom concurrent.futures import ThreadPoolExecutor\nimport random\n#import logging\n\n#logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(name)s - %(levelname)s - %(message)s')\n\nurlList = &#91;'https:\/\/dldir1v6.qq.com\/weixin\/Windows\/WeChatSetup.exe', 'https:\/\/qrsj.gdl.netease.com\/LauncherGame__697327.exe', 'https:\/\/downloadclient03.ztgame.com.cn\/ztmianfei\/client\/zhengtu_setup_20231228.zip', 'https:\/\/pm.myapp.com\/invc\/xfspeed\/qqpcmgr\/download\/QQPCDownload320059.exe', 'https:\/\/d4.a.kwimgs.com\/kos\/nlav12667\/5.35.1\/KwaiLive-Setup-5.35.1.2005-ReleaseFull.exe', 'https:\/\/sta-op.douyucdn.cn\/dypc-client\/pkg\/DYTool\/20240510183635704\/master_DYToolEX_6.3.1.3.472.exe', 'https:\/\/download.huya.com\/huyapc\/install\/HuyaClientInstall.exe']\nthread = 5  #\u7ebf\u7a0b\u6570\u91cf\ngoal = 0  #\u6d88\u8017\u7684\u6d41\u91cf\u5355\u4f4dGB\nif goal > 0:\n    goal = goal * 1024 * 1024 * 1024  #GB\u8f6c\u4e3aB\n\nwasted = 0  #\u5df2\u6d88\u8d39\u7684\u6d41\u91cf\nruning = 0  #\u6b63\u5728\u8fd0\u884c\u7684\u6570\u91cf\n#\u7ebf\u7a0b\u6c60\nexecutor = ThreadPoolExecutor(max_workers=thread)\n#\u4e0b\u8f7d\u8fde\u63a5\u6c60\nsession = requests.Session()\nadapter = requests.adapters.HTTPAdapter(pool_connections=thread, pool_maxsize=thread+1, max_retries=1, pool_block=False)\nsession.mount('http:\/\/', adapter)\nsession.mount('https:\/\/', adapter)\n\n\n\n\n# \u4e0b\u8f7d\u6587\u4ef6\ndef download(url):\n    try:\n        global runing\n        runing += 1\n        response = session.get(url, stream=True, timeout=(10, 30))  # \u8fde\u63a5\u8d85\u65f610\u79d2\uff0c\u8bfb\u53d6\u8d85\u65f630\u79d2\n        if response.status_code == 200:\n            download_start_time = time.time()  # \u5f00\u59cb\u4e0b\u8f7d\u7684\u65f6\u95f4\n            for chunk in response.iter_content(chunk_size=10240):  # \u6309\u5757\u8bfb\u53d6\u6587\u4ef6\u5185\u5bb9\n                if chunk:  # \u786e\u4fddchunk\u4e0d\u4e3a\u7a7a\n                    pass  # \u5904\u7406\u6587\u4ef6\u5185\u5bb9\uff0c\u4f8b\u5982\u5199\u5165\u6587\u4ef6\n\n                # \u68c0\u67e5\u4e0b\u8f7d\u662f\u5426\u8d85\u8fc730\u5206\u949f\n                if time.time() - download_start_time > 1000:  # 1800\u79d2 = 30\u5206\u949f\n                    #print(f\"Download has exceeded 30 minutes: {url}\")\n                    break  # \u7ec8\u6b62\u4e0b\u8f7d\u5faa\u73af\n\n    # except requests.exceptions.RequestException as e:  # \u6355\u83b7requests\u5f02\u5e38\n    #     print(f\"Request failed: {e}\")\n    # except Exception as e:  # \u6355\u83b7\u5176\u4ed6\u5f02\u5e38\n    #     print(f\"An error occurred: {e}\")\n    finally:\n        runing -= 1\n        if response:\n            response.close()  # \u5173\u95ed\u4e0b\u8f7d\u8fde\u63a5\n\n\ndef startDownload():\n    time.sleep(random.randint(1, 10))\n    global wasted\n    wasted = 0\n    # \u5f00\u59cb\u4e0b\u8f7d\n    i = thread\n    while i > 0:\n        executor.submit(download, random.choice(urlList))\n        # \u4f11\u77200.01\u79d2-0.1\u79d2\n        time.sleep(random.randint(1, 10) \/ 100)\n        i -= 1\n\n\nif __name__ == \"__main__\":\n    startDownload()\n    while True:\n        # \u83b7\u53d6\u5f53\u524d\u65f6\u95f4\n        curtime = datetime.datetime.now()\n        hour = curtime.hour\n        if 6 &lt;= hour &lt; 18:\n            if (goal == 0 or wasted &lt; goal) and runing &lt; thread:\n                #logging.info(\"\u8865\u5145\u4e0b\u8f7d\u94fe\u63a5\u6570\uff1a%s\", thread - runing)\n                i = thread - runing\n                while i > 0:\n                    executor.submit(download, random.choice(urlList))\n                    time.sleep(random.randint(1, 10) \/ 100)\n                    i -= 1\n            time.sleep(95)\n        elif 18 &lt;= hour &lt; 24:\n            if (goal == 0 or wasted &lt; goal) and runing &lt; thread:\n                #logging.info(\"\u8865\u5145\u4e0b\u8f7d\u94fe\u63a5\u6570\uff1a%s\", thread - runing)\n                i = thread - runing - 2\n                while i > 0:\n                    executor.submit(download, random.choice(urlList))\n                    time.sleep(random.randint(1, 10) \/ 100)\n                    i -= 1\n            time.sleep(95)\n        else:\n            if (goal==0 or wasted &lt; goal) and runing &lt; thread:\n                #logging.info(\"\u8865\u5145\u4e0b\u8f7d\u94fe\u63a5\u6570\uff1a%s\", thread - runing)\n                i = thread - runing - 1\n                while i> 0:\n                    executor.submit(download, random.choice(urlList))\n                    time.sleep(random.randint(1, 10) \/ 100)\n                    i-=1\n            time.sleep(95)\n\n\n\n\n\n\n\n<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u5728\u673a\u9876\u76d2\u5237\u4e2ahi-nas\uff0c\u7136\u540e\u8fdb\u7ec8\u7aef\uff0c\u88c5python3,\u518d\u5f00\u673a\u5c31\u8fd0\u884cpy\u6587\u4ef6 systemd\u662f\u73b0\u4ee3Linux\u7cfb [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[22],"tags":[],"class_list":["post-1945","post","type-post","status-publish","format-standard","hentry","category-22"],"_links":{"self":[{"href":"http:\/\/blog.xtaa.cn\/index.php\/wp-json\/wp\/v2\/posts\/1945","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/blog.xtaa.cn\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/blog.xtaa.cn\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/blog.xtaa.cn\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/blog.xtaa.cn\/index.php\/wp-json\/wp\/v2\/comments?post=1945"}],"version-history":[{"count":6,"href":"http:\/\/blog.xtaa.cn\/index.php\/wp-json\/wp\/v2\/posts\/1945\/revisions"}],"predecessor-version":[{"id":1960,"href":"http:\/\/blog.xtaa.cn\/index.php\/wp-json\/wp\/v2\/posts\/1945\/revisions\/1960"}],"wp:attachment":[{"href":"http:\/\/blog.xtaa.cn\/index.php\/wp-json\/wp\/v2\/media?parent=1945"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/blog.xtaa.cn\/index.php\/wp-json\/wp\/v2\/categories?post=1945"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/blog.xtaa.cn\/index.php\/wp-json\/wp\/v2\/tags?post=1945"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}