[Python 2.7] Hello World CGI HTTP Server,pythonhello
# CGI HTTP server
## Getting Started
Python 2.x is preferred to this simple demo. I'm using Python 2.7 over windows.
* https://docs.python.org/2.7/library/cgihttpserver.html* https://docs.python.org/2.7/library/cgi.html
## Starting a CGIHTTPServer
$ python -m CGIHTTPServer 8000Serving HTTP on 0.0.0.0 port 8000 ...
## CGI Script, Hello World
$ mkdir cgi-bin/
$ vim cgi-bin/index.pyprint "Content-Type: text/html" # HTML is followingprint # blank line, end of headers
print "<TITLE>CGI script output</TITLE>"print "<H1>This is my first CGI script</H1>"print "Hello, world!"
## Access
Browser the following link and a web page will return.
http://localhost:8000/cgi-bin/index.py
This is my first CGI scriptHello, world!
|
Python轉化為CGI網頁程式
#!/usr/bin/env python
# -*- coding: UTF-8 -*-
# enable debugging
import cgitb; cgitb.enable()
print("Content-Type: text/plain;charset=utf-8")
print()
print("Hello World!")
這是python文檔上的樣本
你要把伺服器配置改一改
把/usr/bin/env python改成你的python的安裝路徑
匯入cgitb模組,就可以做cgi了
另外要注意http的請求返回的格式
伺服器你應該用的是apache把
在httpd.conf裡加上
<Directory "/path">
Options +ExecCGI
AddHandler cgi-script .cgi .py
</Directory>
其中"/path"可以執行cgi程式的檔案夾
AddHandler裡添加可以執行的cgi程式的副檔名。
python 273 與 330 的不同在什地方? 為何在Python273 中輸入 print "Hello, World!"按斷行符號就行
在Python 3.3.0中,已經把print作為一個內建函數了,意思是您輸出的話需要這樣操作:print("Hello, World!"),在Python 3.3.0中print後面的括弧不可以少。而在Python 2.7.3中print後面並不需要括弧。我認為初學者還是應該學習Python 2.7.3,因為目前還有許多模組例如pygame等還不支援Python 3.3.0,而且對於初學者而言,這兩者在準系統上並沒有多大的區別,很多材料都是根據2.7.3之前的版本編寫的,尚未更新到3.3.0,因此您可以放心學習Python 2.7.3.