{"id":1301,"date":"2023-09-19T10:02:20","date_gmt":"2023-09-19T10:02:20","guid":{"rendered":"http:\/\/www.recologypower.com:9080\/?p=1301"},"modified":"2023-09-19T10:02:21","modified_gmt":"2023-09-19T10:02:21","slug":"%e3%80%90openwrt%e3%80%91lua%e7%9a%84cgi%e5%93%8d%e5%ba%94-lua%e4%b8%ad%e8%8e%b7%e5%8f%96get-post%e6%95%b0%e6%8d%ae","status":"publish","type":"post","link":"http:\/\/www.recologypower.com:9080\/?p=1301","title":{"rendered":"\u3010Openwrt\u3011lua\u7684cgi\u54cd\u5e94 lua\u4e2d\u83b7\u53d6GET\/POST\u6570\u636e"},"content":{"rendered":"\n<h2 class=\"wp-block-heading\">\u4e00\u3001lua\u7684cgi\u54cd\u5e94 lua\u4e2d\u83b7\u53d6GET\/POST\u6570\u636e:<\/h2>\n\n\n\n<ol>\n<li>\u5b89\u88c5<a href=\"https:\/\/www.zhihu.com\/search?q=uhttpd&amp;search_source=Entity&amp;hybrid_search_source=Entity&amp;hybrid_search_extra=%7B%22sourceType%22%3A%22answer%22%2C%22sourceId%22%3A283425839%7D\">uhttpd<\/a>\u5305\uff08https\u9700\u8981\u52a0\u88c5\u201cuhttpd-mod-tls\u201d\u548c\u201cpx5g\u201d\uff09\uff1b<\/li>\n\n\n\n<li>\u4fee\u6539uhttpd\u914d\u7f6e\u6587\u4ef6\u201c\/etc\/config\/uhttpd\u201d\uff0c\u5c06Lua\u4f5c\u4e3a\u201cinterpreter\u201d\uff1b<\/li>\n\n\n\n<li>Lua\u811a\u672c\u5199\u5230\u201c\/www\/cgi-bin\/\u201d\u76ee\u5f55\u4e0b\uff0c\u4f46\u662f\u4e0d\u5e26\u6269\u5c55\u540d\uff0c\u4e14\u53ef\u6267\u884c\uff1b<\/li>\n\n\n\n<li>Fast-CGI\u8981\u6c42Lua\u811a\u672c<strong>\u5fc5\u987b\u9996\u5148\u4f7f\u7528\u201cio.write()\u201d\u8f93\u51fa\u201cContent-type: text\/html\\n\\n\u201d<\/strong>\uff1b<\/li>\n\n\n\n<li>\u201c\/www\/cgi-bin\/\u201d\u76ee\u5f55\u4e0b\u7684\u811a\u672c\u53ef\u4ee5\u8c03\u7528\u201c\/usr\/lib\/lua\/\u201d\u76ee\u5f55\u4e0b\u7684\u81ea\u5b9a\u4e49\u6a21\u5757\uff1b<\/li>\n\n\n\n<li>Lua\u73af\u5883\u5df2\u7ecf\u5c31\u7eea\u3002<\/li>\n<\/ol>\n\n\n\n<h2 class=\"wp-block-heading\">\u4e8c\u3001\u4ee5<strong>\u201c\u8bfb\u53d6GET\u548cPOST\u6570\u636e\u201d<\/strong>\u4e3a\u4f8b\uff0c\u5168\u90e8\u4f7f\u7528\u9ed8\u8ba4\u503c\u7684\u60c5\u51b5\u4e0b\uff0c\u5982\u4f55\u5177\u4f53\u5b9e\u73b0\uff1a<\/h2>\n\n\n\n<p>\u7b2c\u4e00\u6b65\uff1a\u4f7f\u7528\u201copkg update\u201d\u66f4\u65b0\u201c.ipk\u201d\u5305\u7684\u6e90\uff0c\u7136\u540e\u4f7f\u7528\u201copkg install uhttpd\u201d\u5b89\u88c5\uff1b<\/p>\n\n\n\n<p>\u7b2c\u4e8c\u6b65\uff1a\u4f7f\u7528\u201cvi \/etc\/config\/uhttpd\u201d\uff0c\u5728\u201cconfig uhttpd main\u201d\u4e0b\u6dfb\u52a0\u4e00\u884c<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>list interpreter '.lua=\/usr\/bin\/lua'<\/code><\/pre>\n\n\n\n<p>\u7b2c\u4e09\u6b65\uff1a\u5efa\u7acb\u76ee\u5f55\u201c\/www\/cgi-bin\u201d\uff0c\u5e76\u589e\u52a0\u201c+x\u201d\u5c5e\u6027\uff1b<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>root@OpenWrt:~# mkdir -p \/www\/cgi-bin\/\r\nroot@OpenWrt:~# chmod +x \/www\/cgi-bin\/<\/code><\/pre>\n\n\n\n<p>\u5efa\u7acb\u54cd\u5e94\u6587\u4ef6\uff0c\u5e76\u589e\u52a0\u201c+x\u201d\u5c5e\u6027\uff1b<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>root@OpenWrt:~# touch \/www\/cgi-bin\/webservice\r\nroot@OpenWrt:~# chmod +x \/www\/cgi-bin\/webservice<\/code><\/pre>\n\n\n\n<p>\u5c06\u6587\u4ef6\u201c\/www\/cgi-bin\/<a rel=\"noreferrer noopener\" href=\"https:\/\/so.csdn.net\/so\/search?q=webservice&amp;spm=1001.2101.3001.7020\" target=\"_blank\">webservice<\/a>\u201d\u5185\u5bb9\u4fee\u6539\u4e3a\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>#!\/usr\/bin\/lua\r\nlocal WebService = require 'WebService'\r\nWebService.Run()<\/code><\/pre>\n\n\n\n<p>\u7b2c\u56db\u6b65\uff1a\u5efa\u7acb<a rel=\"noreferrer noopener\" href=\"https:\/\/so.csdn.net\/so\/search?q=Lua&amp;spm=1001.2101.3001.7020\" target=\"_blank\">Lua<\/a>\u6a21\u5757\u6587\u4ef6\uff08\u4e0d\u518d\u8981\u6c42\u201c+x\u201d\u5c5e\u6027\uff09\uff0c\u5e76\u8bfb\u53d6\u53c2\u6570\uff0c\u8fd4\u56de\u54cd\u5e94\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>root@OpenWrt:~# touch \/usr\/lib\/lua\/WebService.lua<\/code><\/pre>\n\n\n\n<p>\u5c06\u5176\u5185\u5bb9\u4fee\u6539\u4e3a\u5982\u4e0b\u5185\u5bb9\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>local WebService = {}\r\n \r\nfunction WebService.Run()\r\n    local client = os.getenv(\"REMOTE_ADDR\")\r\n    local GET = os.getenv(\"QUERY_STRING\")\r\n    local POST = nil\r\n    local POSTLength = tonumber(os.getenv(\"CONTENT_LENGTH\")) or 0\r\n    if (POSTLength > 0) then\r\n        POST = io.read(POSTLength)\r\n        --POST = io.read(\"*a\")\r\n    end\r\n \r\n    -- Fast-CGI+HTTP require HEADER\r\n    -- enable cache\r\n    --io.write(\"Content-type: text\/html\\n\\n\")\r\n    -- disable cache, especially for Internet Explorer\r\n    io.write(\"Content-type: text\/html\\nPragma: no-cache\\n\\n\")\r\n \r\n    local reply = string.format(\"Client %s said: url: &#91;%s], data: &#91;%s]\\n\", client or '-', GET or '-', POST or '-')\r\n    io.write(reply)\r\nend\r\n \r\nreturn WebService<\/code><\/pre>\n\n\n\n<p>\u7b2c\u4e94\u6b65\uff1a\u91cd\u542f\u201cuhttpd\u201d\u670d\u52a1\uff08\u4e3a\u4e86\u8ba9\u66f4\u6539\u7684\/etc\/config\/uhttpd\u751f\u6548\uff09\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>root@OpenWrt:~# \/etc\/init.d\/uhttpd restart<\/code><\/pre>\n\n\n\n<p>\u7b2c\u516d\u6b65\uff1a\u4f7f\u7528\u6d4f\u89c8\u5668\u6d4b\u8bd5\uff1a<\/p>\n\n\n\n<p>\u4f7f\u7528\u6d4f\u89c8\u5668\u8bbf\u95ee\u5982\u4e0b\u5730\u5740\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>http:\/\/&lt;openwrt_ipaddr>\/cgi-bin\/webservice?author=qige\n<\/code><\/pre>\n\n\n\n<p>\u8fd9\u91cc\u5047\u8bbeOpenWrt\u5f00\u53d1\u677f\u7684IP\u4e3a192.168.1.24\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>http:&#47;&#47;192.168.1.1\/cgi-bin\/webservice?author=qige<\/code><\/pre>\n\n\n\n<p>\u6700\u7ec8\u6548\u679c\u5982\u4e0b\u56fe\uff1a<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img decoding=\"async\" loading=\"lazy\" width=\"926\" height=\"469\" src=\"http:\/\/www.recologypower.com:9080\/wp-content\/uploads\/2023\/09\/00c98eb27bcef580d85fb6b651715542.png\" alt=\"\" class=\"wp-image-1302\" srcset=\"http:\/\/www.recologypower.com:9080\/wp-content\/uploads\/2023\/09\/00c98eb27bcef580d85fb6b651715542.png 926w, http:\/\/www.recologypower.com:9080\/wp-content\/uploads\/2023\/09\/00c98eb27bcef580d85fb6b651715542-300x152.png 300w, http:\/\/www.recologypower.com:9080\/wp-content\/uploads\/2023\/09\/00c98eb27bcef580d85fb6b651715542-768x389.png 768w, http:\/\/www.recologypower.com:9080\/wp-content\/uploads\/2023\/09\/00c98eb27bcef580d85fb6b651715542-600x304.png 600w\" sizes=\"(max-width: 926px) 100vw, 926px\" \/><\/figure>\n\n\n\n<p>\u901a\u8fc7cUrl\u6a21\u62dfPOST\u63d0\u4ea4<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img decoding=\"async\" loading=\"lazy\" width=\"531\" height=\"79\" src=\"http:\/\/www.recologypower.com:9080\/wp-content\/uploads\/2023\/09\/7cea1153da369ef9c2edab94c5b12bc5.png\" alt=\"\" class=\"wp-image-1303\" srcset=\"http:\/\/www.recologypower.com:9080\/wp-content\/uploads\/2023\/09\/7cea1153da369ef9c2edab94c5b12bc5.png 531w, http:\/\/www.recologypower.com:9080\/wp-content\/uploads\/2023\/09\/7cea1153da369ef9c2edab94c5b12bc5-300x45.png 300w\" sizes=\"(max-width: 531px) 100vw, 531px\" \/><\/figure>\n\n\n\n<p>\u901a\u8fc7form\u8868\u5355POST\u63d0\u4ea4\u7684\u6d4f\u89c8\u5668\u7ed3\u679c<\/p>\n\n\n\n<p>\u6ce8\u610f\uff1a<\/p>\n\n\n\n<ol>\n<li>POST\u6570\u636e\u53ef\u4ee5\u7531curl\u3001Python requests\u7b49\u8fdb\u884c\u6a21\u62df\u63d0\u4ea4\uff0c\u4e5f\u53ef\u4ee5\u81ea\u5236\u5982\uff1a&lt;form action=&#8221;\/cgi-bin\/webservice&#8221; method=&#8221;POST&#8221;&gt;\u63d0\u4ea4\uff1b<\/li>\n\n\n\n<li>\u6839\u636eFast-CGI\u8981\u6c42\uff0c\u5fc5\u987b\u8981\u5148\u4f7f\u7528\u201cio.write()\u201d\u8f93\u51fahttp\u534f\u8bae\u5934\u90e8\uff1b\u518d\u6839\u636ehttp\u534f\u8bae\u7684\u8981\u6c42\uff0c\u5934\u90e8\u548c\u5185\u5bb9\u4e4b\u95f4\u5fc5\u987b\u4f7f\u7528\u201c\\n\\n\u201d\u6765\u9694\u5f00\u3002\u56e0\u6b64Lua\u811a\u672c\u5fc5\u987b\u5148\u8f93\u51fa\u5934\u90e8\u201cContent-type: text\/html\\n\\n\u201d\uff0c\u518d\u8f93\u51fa\u5176\u5b83\u5185\u5bb9\uff1b<\/li>\n\n\n\n<li>\u5982\u679c\u201c\u4e0d\u9700\u8981\u7f13\u5b58\u201d\uff0c\u8f93\u51fa\u7acb\u5373\u8fc7\u671f\uff0c\u5219\u4fee\u6539\u5934\u90e8\u5185\u5bb9\u4e3a\u201cContent-type: text\/html\\nPragma: no-cache\\n\\n\u201d\uff1b<\/li>\n\n\n\n<li>\u8ba9uhttpd\u5f00\u673a\u81ea\u52a8\u542f\u52a8\uff0c\u9700\u8981\u6267\u884c\u201c\/etc\/init.d\/uhttpd enable\u201d\uff1b<\/li>\n\n\n\n<li>\u9700\u8981\u5c06\u63a5\u6536\u5230\u7684\u6570\u636e\u8f93\u51fa\u5230\u4e32\u53e3\uff0c\u53ef\u4ee5\u518d\u5b89\u88c5\u4e00\u4e2a\u201ccoreutils-stty\u201d\u5305\u7528\u4e8e\u63a7\u5236\u4e32\u53e3\u6ce2\u7279\u7387<\/li>\n<\/ol>\n\n\n\n<pre class=\"wp-block-code\"><code>root@OpenWrt:~# opkg install coreutils\r\nroot@OpenWrt:~# opkg install coreutils-stty<\/code><\/pre>\n\n\n\n<p>\u518d\u5c06\u4ee5\u4e0b\u5185\u5bb9\u6dfb\u52a0\u5230Lua\u6a21\u5757\u7684\u9002\u5f53\u4f4d\u7f6e\uff0c\u5e76\u8c03\u7528\u6b64\u51fd\u6570\u5373\u53ef\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>function WebService.uartWrite(msg)\r\n    local uartConfig = 'stty -F \/dev\/ttyS0 raw speed 9600\\n'\r\n    os.execute(uartConfig)\r\n \r\n    local cmd = string.format(\"echo '%s' > \/dev\/ttyS0\\n\", msg or '')\r\n    os.execute(cmd)\r\nend<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<div class=\"slide-text-bg2\">\n<h3>\u4e00\u3001lua\u7684cgi\u54cd\u5e94 lua\u4e2d\u83b7\u53d6GE<\/h3>\n<\/div>\n<div class=\"flex-btn-div\"><a href=\"http:\/\/www.recologypower.com:9080\/?p=1301\" class=\"btn1 flex-btn\">\u9605\u8bfb\u66f4\u591a<\/a><\/div>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[77],"tags":[],"_links":{"self":[{"href":"http:\/\/www.recologypower.com:9080\/index.php?rest_route=\/wp\/v2\/posts\/1301"}],"collection":[{"href":"http:\/\/www.recologypower.com:9080\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.recologypower.com:9080\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.recologypower.com:9080\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"http:\/\/www.recologypower.com:9080\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1301"}],"version-history":[{"count":1,"href":"http:\/\/www.recologypower.com:9080\/index.php?rest_route=\/wp\/v2\/posts\/1301\/revisions"}],"predecessor-version":[{"id":1304,"href":"http:\/\/www.recologypower.com:9080\/index.php?rest_route=\/wp\/v2\/posts\/1301\/revisions\/1304"}],"wp:attachment":[{"href":"http:\/\/www.recologypower.com:9080\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1301"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.recologypower.com:9080\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1301"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.recologypower.com:9080\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1301"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}