[Python 2.7] Hello World CGI HTTP Server,pythonhello

來源:互聯網
上載者:User

[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.
 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.