在mac os下的Apache伺服器的cgi中運行python

來源:互聯網
上載者:User

標籤:

我是搬運工。。Running Python Programs on the Mac OS X Apache Web Server

The Mac OS X operating system includes a pre-configured Apache web server and also includes the libraries needed to run Python. Thus, Python CGI scripts can be run without any configuration changes to the Apache configuration as long as the scripts are placed in the correct location and given the correct file permissions. 

This guide assumes you are using OS X Leopard, although the steps should be the same or similar for earlier versions of OS X.

The default location to place CGI programs for the OS X apache installation is in the following directory: 

/Library/WebServer/CGI-Executables 

Files placed in this directory can be accessed via the following URL: 

http://localhost/cgi-bin/file-name

In order for Apache to be able to execute the CGI script, the permissions on the file must be set correctly. For example, open EditRocket and create a file called test.py with the following contents. Save this file to the /Library/WebServer/CGI-Executables directory.

#!/usr/bin/pythonprint "Content-type: text/html"printprint "<html><head>"print ""print "</head><body>"print "Test Page"print "</body></html>"

Open a terminal window (Applications -> Utilities -> Terminal). Execute the following command: 

cd /Library/WebServer/CGI-Executables 

Then execute the following (if prompted, enter your password) 

sudo chmod 755 test.py 

You now have a Python program, test.py, saved with the correct permissions in the /Library/WebServer/CGI-Executables directory.  

Now, we need to restart the web server to make sure the changes take effect. To restart, do the following:

1. Go to System Preferences -> Sharing View 

2. Uncheck the Web Sharing box. Then check the Web Sharing box again. This will start and stop the apache web server. 

You can test this page by entering the following in your web browser: 

http://localhost/cgi-bin/test.py

在mac os下的Apache伺服器的cgi中運行python

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.