« »
2008-05-03Python

56

mod_python及psp开发配置

在apache的配置文件httpd.conf追加

<Directory /some/path>
   AddHandler mod_python .psp
   PythonHandler mod_python.psp
   PythonDebug On
</Directory>
<IfModule mod_dir.c>
    DirectoryIndex index.psp index.html index.htm
</IfModule>

mod_python下载地址:http://httpd.apache.org/modules/python-download.cgi 选择自己需要的版本。


How can I print out data?

<%
# we are going to print out data in two ways
test_string = “Hello World!”
%>
<html>
    <%– Output using the expression delimiters –%>
    <%= test_string %>
    <br/>
    This is some static content in the page…
    <br/>
    <%– Output using the request object –%>
    <% req.write(”Hello again!”) %>
</html> 

How can I get a variable that is passed on the URL?

<html>
<%
# get the variable ‘page’ passed on the URL
if form.has_key(”page”):
    page = form[”page”]
else:
    page = “home”
req.write(”You are on the ” + page + ” page.”)
%>
</html> 

How can I tell what browser someone is using?

<html>
<%
# get the client’s user agent string
req.add_common_vars() # init the CGI variables
if req.subprocess_env.has_key(”HTTP_USER_AGENT”):
    agent = req.subprocess_env[”HTTP_USER_AGENT”]
    req.write(”You are using ” + agent + “.”)
else:
    req.write(”Couldn’t determine user agent”)
# get out of the else
%>
</html> 

How can I save variables in a cookie for later use?

<html>
<%
from mod_python.Cookie import add_cookie, get_cookies, Cookie
# check if we are explicitly passed a style
style = “default”
if form.has_key(”style”):
    # set the style and try to save it
    style = form[”style”]
    style_cookie = Cookie(”style”, style)
    # uncomment the next line to make the cookie “permanent”
    #style_cookie.expires = 2114398800
    # save the cookie
    add_cookie(req, style_cookie)
else:
    # try to load the style from a saved cookie
    page_cookies = get_cookies(req, Cookie)
    if page_cookies.has_key(”style”):
    style_cookie = page_cookies[”style”]
    style = style_cookie.value
req.write(”You are using the ” + style + ” style.”)
%>
<br/>
Change style: <a href=”?style=default”>Default</a> <a href=”?style=ice”>Ice</a> <a href=”?style=gentoo”>Gentoo</a>
<br/>
Remove the style option in the URL and the selected value should be loaded from a cookie when you reload the page.
</html>

您还可能感兴趣的内容

日志信息 »

该日志于2008-05-03 07:12由 x72 发表在Python分类下, 通告目前不可用,你可以至底部留下评论。

没有评论

发表评论 »


返回顶部