{"id":195,"date":"2013-03-11T16:23:09","date_gmt":"2013-03-11T23:23:09","guid":{"rendered":"https:\/\/www.uniteng.com\/?p=195"},"modified":"2021-01-05T14:45:46","modified_gmt":"2021-01-05T06:45:46","slug":"python-cgi-script-with-lighttpd","status":"publish","type":"post","link":"https:\/\/www.uniteng.com\/index.php\/2013\/03\/11\/python-cgi-script-with-lighttpd\/","title":{"rendered":"Python CGI script with lighttpd"},"content":{"rendered":"<p><strong>1.Edit lighttpd.conf<\/strong><\/p>\n<pre class=\"brush:cpp;\">$sudo vim \/etc\/lighttpd\/lighttpd.conf\r\n\r\n1. Change\r\ncgi.assign = (\".php\" => \"\/usr\/bin\/php5-cgi\")\r\nserver.error-handler-404 = \"\/index.html\"\r\n\r\nTo\r\ncgi.assign = (\".php\" => \"\/usr\/bin\/php5-cgi\",\r\n              \".py\"  => \"\/usr\/bin\/python\"\r\n\t     )\r\nserver.error-handler-404 = \"\/index.html\"\r\n\r\n2. Change\r\nstatic-file.exclude-extensions = ( \".php\", \".pl\", \".fcgi\")\r\n\t\r\nTo\r\nstatic-file.exclude-extensions = ( \".php\", \".pl\", \".fcgi\", \".py\")\r\n\t\r\n3. Change\r\nindex-file.names            = ( \"index.php\", \"index.html\", \"index.lighttpd.html\" )\r\n\t\r\nTo\r\nindex-file.names            = ( \"index.php\", \"index.html\", \"index.lighttpd.html\",\"index.py\" )\r\n\r\n4. sudo \/etc\/init.d\/lighttpd force-reload\r\n<\/pre>\n<p><strong>2. Testing CGI Script<\/strong><\/p>\n<pre class=\"brush:cpp;\">\r\n1. In dictionary \/var\/www\/cgi\r\nsudo vim hellopy.py\r\n\t\r\nprint \"Content-Type: text\/html\"\r\nprint\r\nprint \"<TITLE>CGI script output<\/TITLE>\"\r\nprint \"<H1>This is my first CGI script<\/H1>\"\r\nprint \"Hello, world!\"\r\n\r\n2. Access http:\/\/your_IP_address\/cgi\/hellopy.py\r\nYou will see following content:\r\n\r\nThis is my first CGI script\r\nHello, world! \r\n<\/pre>\n<p><strong>3. CGI script with root privileges<\/strong><\/p>\n<pre class=\"brush:cpp;\">$ sudo visudo\r\n\r\nthen add the following line at the end of file :\r\nwww-data ALL=(ALL) NOPASSWD: ALL\r\n<\/pre>\n<p>Note: This configuration may lead to security problem. This configuration is okay for debug, but not suitable for daily use.<\/p>\n<p><a href=\"http:\/\/www.mobilewill.us\/2012\/07\/raspberry-pi-and-gpio-permissions.html\" target=\"_blank\" rel=\"noopener\">A better way to configure CGI script with root privileges<\/a><\/p>\n<p><strong>4. Testing CGI Script with root privileges<\/strong><\/p>\n<pre class=\"brush:cpp;\">import os\r\ntmp = os.popen(\"sudo \/opt\/vc\/bin\/vcgencmd measure_temp | grep -o '[0-9.]*'\").readlines()\r\ntmp_num=float(tmp[0])\r\nprint \"Content-Type: text\/html\"\r\nprint \"%f\" %tmp_num\r\n<\/pre>\n<p>This CGI script will return the temperature of the CPU on the Raspberry Pi.<br \/>\n<a href=\"https:\/\/www.uniteng.com\/wp-content\/uploads\/2013\/03\/CGIwithRoot.png\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/www.uniteng.com\/wp-content\/uploads\/2013\/03\/CGIwithRoot.png\" alt=\"CGIwithRoot\" width=\"388\" height=\"90\" class=\"alignnone size-full wp-image-207\" srcset=\"https:\/\/www.uniteng.com\/wp-content\/uploads\/2013\/03\/CGIwithRoot.png 388w, https:\/\/www.uniteng.com\/wp-content\/uploads\/2013\/03\/CGIwithRoot-300x69.png 300w\" sizes=\"auto, (max-width: 388px) 100vw, 388px\" \/><\/a><br \/>\nNote: <a href=\"http:\/\/elinux.org\/RPI_vcgencmd_usage\" target=\"_blank\" rel=\"noopener\">vcgencmd command document<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>1.Edit lighttpd.conf $sudo vim \/etc\/lighttpd\/lighttpd.conf 1. Change cgi.assign = (&#8220;.php&#8221; => &#8220;\/usr\/bin\/php5-cgi&#8221;) server.error-handler-404 = &#8220;\/index.html&#8221; To cgi.assign = (&#8220;.php&#8221; => &#8220;\/usr\/bin\/php5-cgi&#8221;, &#8220;.py&#8221; => &#8220;\/usr\/bin\/python&#8221; ) server.error-handler-404 = &#8220;\/index.html&#8221; 2. Change static-file.exclude-extensions = ( &#8220;.php&#8221;, &#8220;.pl&#8221;, &#8220;.fcgi&#8221;) To static-file.exclude-extensions = ( &#8220;.php&#8221;, &#8220;.pl&#8221;, &#8220;.fcgi&#8221;, &#8220;.py&#8221;) 3. Change index-file.names = ( &#8220;index.php&#8221;, &#8220;index.html&#8221;, &#8220;index.lighttpd.html&#8221; ) To index-file.names [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_uag_custom_page_level_css":"","site-sidebar-layout":"default","site-content-layout":"","ast-site-content-layout":"default","site-content-style":"default","site-sidebar-style":"default","ast-global-header-display":"","ast-banner-title-visibility":"","ast-main-header-display":"","ast-hfb-above-header-display":"","ast-hfb-below-header-display":"","ast-hfb-mobile-header-display":"","site-post-title":"","ast-breadcrumbs-content":"","ast-featured-img":"","footer-sml-layout":"","ast-disable-related-posts":"","theme-transparent-header-meta":"","adv-header-id-meta":"","stick-header-meta":"","header-above-stick-meta":"","header-main-stick-meta":"","header-below-stick-meta":"","astra-migrate-meta-layouts":"default","ast-page-background-enabled":"default","ast-page-background-meta":{"desktop":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"ast-content-background-meta":{"desktop":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"ngg_post_thumbnail":0,"footnotes":""},"categories":[11],"tags":[6,9,8,7],"class_list":["post-195","post","type-post","status-publish","format-standard","hentry","category-software-notes","tag-raspberrypi","tag-rs232","tag-serial-port","tag-universal-expansion-board"],"uagb_featured_image_src":{"full":false,"thumbnail":false,"medium":false,"medium_large":false,"large":false,"1536x1536":false,"2048x2048":false},"uagb_author_info":{"display_name":"neilhao","author_link":"https:\/\/www.uniteng.com\/index.php\/author\/neilhao\/"},"uagb_comment_info":0,"uagb_excerpt":"1.Edit lighttpd.conf $sudo vim \/etc\/lighttpd\/lighttpd.conf 1. Change cgi.assign = (\".php\" => \"\/usr\/bin\/php5-cgi\") server.error-handler-404 = \"\/index.html\" To cgi.assign = (\".php\" => \"\/usr\/bin\/php5-cgi\", \".py\" => \"\/usr\/bin\/python\" ) server.error-handler-404 = \"\/index.html\" 2. Change static-file.exclude-extensions = ( \".php\", \".pl\", \".fcgi\") To static-file.exclude-extensions = ( \".php\", \".pl\", \".fcgi\", \".py\") 3. Change index-file.names = ( \"index.php\", \"index.html\", \"index.lighttpd.html\" ) To index-file.names&hellip;","_links":{"self":[{"href":"https:\/\/www.uniteng.com\/index.php\/wp-json\/wp\/v2\/posts\/195","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.uniteng.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.uniteng.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.uniteng.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.uniteng.com\/index.php\/wp-json\/wp\/v2\/comments?post=195"}],"version-history":[{"count":0,"href":"https:\/\/www.uniteng.com\/index.php\/wp-json\/wp\/v2\/posts\/195\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.uniteng.com\/index.php\/wp-json\/wp\/v2\/media?parent=195"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.uniteng.com\/index.php\/wp-json\/wp\/v2\/categories?post=195"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.uniteng.com\/index.php\/wp-json\/wp\/v2\/tags?post=195"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}