I am using the standard plan on Sakura's rental server. Create a simple "Hello World" program written in Python I will explain how to operate as CGI.
Hello.cgi
#!/usr/local/bin/python
print "Content-Type: text/html\n\n"
print "Hello World"
It must be "cgi" instead of "py" (* 1).
Set the character code to "UTF-8" It is good to set the line feed symbol to "LF". It seems that an error may occur with other combinations.
The command path to python described in the first line of "Hello.cgi" above. For the latest information, please check the item "Program Command Path" on the Sakura Support Page> "Rental Server Service Specifications"> "About CGI / PHP / SSI" → Sakura Support Page
The created file "Hello.cgi" can be uploaded anywhere under the document root (/ home / account name / www /), or it can be a newly created folder under the document root.
File permissions must be set to 755, 705, or 700 (* 2).
Also, if the file "Hello.cgi" is uploaded in the folder created under the document root, the folder permission for the folder containing the file "Hello.cgi" is 755, 705, or 700 (* 2). Must be set to.
".Htaccess" is not required.
However, as you pointed out in the comment section, If this is left as it is, the extension "py" must be changed to "cgi", which is inefficient. So, create .htaccess with the following description Allows it to operate as cgi even with the extension "py" There is also a method called.
.htaccess
AddType application/x-httpd-cgi .py
The latest information is Sakura support page> "Rental server service specifications"> "About CGI / PHP / SSI" page, there is an item "File and directory settings", so please check here → Sakura support page //help.sakura.ad.jp/206206041/)
Specify the URL where "hello.cgi" is located in the browser Confirm that "Hello World" is displayed. URL specification example: If you place "hello.cgi" in the document root, It will be "https: // (domain name) /hello.cgi".
When I tried CGI in Python for a moment, It would be nice to have a simple example! I thought, so I made this post. I hope it helps someone.
Recommended Posts