{"id":971,"date":"2022-07-18T19:39:09","date_gmt":"2022-07-18T19:39:09","guid":{"rendered":"https:\/\/sznetwork.systems\/?p=971"},"modified":"2025-06-28T11:30:27","modified_gmt":"2025-06-28T09:30:27","slug":"writing-python-web-service-to-power-off-or-reboot-raspberry-pi","status":"publish","type":"post","link":"https:\/\/sznetwork.systems\/?p=971","title":{"rendered":"Writing python web service to power off or reboot Raspberry PI.(Migrated)"},"content":{"rendered":"\n<figure class=\"wp-block-embed is-type-video is-provider-youtube wp-block-embed-youtube wp-embed-aspect-16-9 wp-has-aspect-ratio\"><div class=\"wp-block-embed__wrapper\">\n<iframe loading=\"lazy\" title=\"Writing python web service to power off or reboot raspberry pi.\" width=\"500\" height=\"281\" src=\"https:\/\/www.youtube.com\/embed\/8BsPeCtpPCU?feature=oembed\" frameborder=\"0\" allow=\"accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share\" referrerpolicy=\"strict-origin-when-cross-origin\" allowfullscreen><\/iframe>\n<\/div><\/figure>\n\n\n\n<pre class=\"wp-block-preformatted has-black-color has-cyan-bluish-gray-background-color has-text-color has-background\">Small web service to power off or reboot remotely Linux based PC like Orange Pi or Raspberry Pi PC also it works on x86 based PCs.<\/pre>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"552\" src=\"https:\/\/sznetwork.systems\/wp-content\/uploads\/2022\/07\/Screenshot_2022-07-11_23-47-53-1-1024x552.jpg\" alt=\"\" class=\"wp-image-978\" srcset=\"https:\/\/sznetwork.systems\/wp-content\/uploads\/2022\/07\/Screenshot_2022-07-11_23-47-53-1-1024x552.jpg 1024w, https:\/\/sznetwork.systems\/wp-content\/uploads\/2022\/07\/Screenshot_2022-07-11_23-47-53-1-300x162.jpg 300w, https:\/\/sznetwork.systems\/wp-content\/uploads\/2022\/07\/Screenshot_2022-07-11_23-47-53-1-768x414.jpg 768w, https:\/\/sznetwork.systems\/wp-content\/uploads\/2022\/07\/Screenshot_2022-07-11_23-47-53-1.jpg 1113w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<pre class=\"wp-block-preformatted has-black-color has-cyan-bluish-gray-background-color has-text-color has-background\">Compiled program can be run by command <strong>sudo .\/SZ_WPowerM<\/strong><\/pre>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"324\" height=\"53\" src=\"https:\/\/sznetwork.systems\/wp-content\/uploads\/2022\/07\/Screenshot_2022-07-11_23-48-57.jpg\" alt=\"\" class=\"wp-image-975\" srcset=\"https:\/\/sznetwork.systems\/wp-content\/uploads\/2022\/07\/Screenshot_2022-07-11_23-48-57.jpg 324w, https:\/\/sznetwork.systems\/wp-content\/uploads\/2022\/07\/Screenshot_2022-07-11_23-48-57-300x49.jpg 300w\" sizes=\"auto, (max-width: 324px) 100vw, 324px\" \/><\/figure>\n\n\n\n<h2 class=\"has-black-color has-text-color has-background wp-block-heading\" style=\"background-color:#8e8f8f\">Code of project<\/h2>\n\n\n\n<pre class=\"wp-block-preformatted has-black-color has-cyan-bluish-gray-background-color has-text-color has-background\">import os\nfrom bottle import route, get, post, run, template\n\n\n\n@post('\/power')\ndef power():\n\tos.system('shutdown -h now')\n@post('\/restart')\ndef reboot():\n\tos.system('reboot')\n@route('\/')\ndef index():\n\treturn template('''&lt;!DOCTYPE html&gt;\n&lt;html&gt;\n&lt;head&gt;\n&lt;title&gt;SZ_WPowerR&lt;\/title&gt;\n&lt;meta name=\"viewport\" content=\"width=device-width, initial-scale=1.0\"&gt;\n&lt;script&gt;\nfunction power(){\nvar link = window.location.href + \"power\";\nvar xhttp = new XMLHttpRequest();\nxhttp.open(\"POST\", link);\nxhttp.send();\n}\nfunction restart(){\nvar link = window.location.href + \"restart\";\nvar xhttp = new XMLHttpRequest();\nxhttp.open(\"POST\", link);\nxhttp.send();\n}\n&lt;\/script&gt;\n&lt;style&gt;\na:hover{\ncolor: #5c6847;\n}\na{\ncolor: #062b00;\ntext-decoration: none;\n}\n.footer {\n    position: fixed;\n    bottom: 0;\n    right: 15px;\n}\n.center {\n  margin: 0;\n  position: absolute;\n  top: 50%;\n  left: 50%;\n  -ms-transform: translate(-50%, -50%);\n  transform: translate(-50%, -50%);\n}\n.button {\n  background-color: #062b00;\n  border: none;\n  color: white;\n  padding: 15px 32px;\n  text-align: center;\n  text-decoration: none;\n  display: inline-block;\n  font-size: 16px;\n  margin: 4px 2px;\n  cursor: pointer;\n  width: 100%;\n}\n.button:hover\n{\ncolor: #5c6847;\n}\nbody {\n background-color: #e8d7c2;\n}\nh1 {\n  text-align: center;\n  color: #062b00;\n}\np{\ncolor: #062b00;\n}\n&lt;\/style&gt;\n&lt;\/head&gt;\n&lt;body&gt;\n&lt;h1&gt;SZ_WPowerR&lt;\/h1&gt;\n &lt;div class=\"center\"&gt;\n&lt;p&gt;Choose action:&lt;\/p&gt; \n&lt;button class=\"button\" onclick=\"restart()\"&gt;Reboot&lt;\/button&gt;\n&lt;button class=\"button\" onclick=\"power()\"&gt;Power Off&lt;\/button&gt;\n&lt;\/div&gt;\n&lt;div class=\"footer\"&gt;\n&lt;p&gt;&lt;a href=\"https:\/\/sznetwork.systems\"&gt;sznetwork.systems&lt;\/a&gt;&lt;\/p&gt;\n&lt;\/div&gt;\n&lt;\/body&gt;\n&lt;\/html&gt;\n    ''')\n\nrun(host='0.0.0.0', port=8080)\n<\/pre>\n\n\n\n<h2 class=\"has-black-color has-text-color has-background wp-block-heading\" style=\"background-color:#8f8f8f\">Link to download source code and compiled file for x86<\/h2>\n\n\n\n<pre class=\"wp-block-preformatted has-black-color has-cyan-bluish-gray-background-color has-text-color has-background\"><a href=\"https:\/\/sznetwork.systems\/wp-content\/uploads\/2022\/07\/Code_SZ_WPowerM.zip\" data-type=\"URL\" data-id=\"https:\/\/sznetwork.systems\/wp-content\/uploads\/2022\/07\/Code_SZ_WPowerM.zip\">Download source &amp; compiled<\/a><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Small web service to power off or reboot remotely Linux based PC like Orange Pi or Raspberry Pi PC also it works on x86 based PCs. Compiled program can be. <a class=\"read-more-link\" href=\"https:\/\/sznetwork.systems\/?p=971\">read more&#8230;<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[24,319],"tags":[165,154,155,161,157,163,158,151,150,148,149,160,162,164,159,166,152,153,156],"class_list":["post-971","post","type-post","status-publish","format-standard","hentry","category-software","category-sz-projects","tag-how-to-remote-shutdown-using-ip-address","tag-how-to-remotely-shutdown-any-computer","tag-how-to-restart-or-shutdown-a-remote-computer","tag-orange-pi-computer","tag-orange-pi-pc","tag-orange-pi-pc-web-server-at-home","tag-orange-pi-web-server","tag-python-bottle","tag-python-web-server","tag-python-web-server-get-post","tag-python-web-server-post","tag-raspberry-pi-4-as-web-server","tag-raspberry-pi-http-server","tag-raspberry-pi-pc-web-server","tag-raspberry-pi-web-server","tag-remotely-shutdown-any-computer","tag-shutdown-from-web","tag-shutdown-from-web-api","tag-source-code"],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/sznetwork.systems\/index.php?rest_route=\/wp\/v2\/posts\/971","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/sznetwork.systems\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/sznetwork.systems\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/sznetwork.systems\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/sznetwork.systems\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=971"}],"version-history":[{"count":10,"href":"https:\/\/sznetwork.systems\/index.php?rest_route=\/wp\/v2\/posts\/971\/revisions"}],"predecessor-version":[{"id":1943,"href":"https:\/\/sznetwork.systems\/index.php?rest_route=\/wp\/v2\/posts\/971\/revisions\/1943"}],"wp:attachment":[{"href":"https:\/\/sznetwork.systems\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=971"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/sznetwork.systems\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=971"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/sznetwork.systems\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=971"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}