亚洲av成人无遮挡网站在线观看,少妇性bbb搡bbb爽爽爽,亚洲av日韩精品久久久久久,兔费看少妇性l交大片免费,无码少妇一区二区三区

Chinaunix

標題: 再問:python server page中有沒有類似php的header函數 [打印本頁]

作者: crykun    時間: 2012-04-14 18:31
標題: 再問:python server page中有沒有類似php的header函數
本帖最后由 crykun 于 2012-04-14 18:34 編輯

萬能的cu,再次求助。

mod_python3.3.1 rhel 5.4  apache2.0.64環(huán)境,寫的psp訪問數據庫的程序,

中文字符顯示的是亂碼,刷新一次,又正常了。關閉瀏覽器,輸入網址,又是亂碼,刷新一次,又正常。

初步排除是數據庫字符集和meta <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />

想寫一個php類似的header()改變http的報頭,不知怎么寫,可有朋友知道,這是什么原因?或是問題所在?

小弟初學,高人不要笑。


下面是psp程序,一個讀取oracle記錄的演示程序:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>python oracle中文頁</title>
</head>
<body>


<%

import cx_Oracle
import os


req.write('Hello world!')
req.write('<hr>')
req.write('python oracle 中文頁')
req.write('<hr>')

os.environ['NLS_LANG'] = 'SIMPLIFIED CHINESE_CHINA.UTF8'


db = cx_Oracle.connect('system', 'mz123456', '192.168.1.119/orcl')
c = db.cursor()
c.execute('SELECT id,xname FROM mytab order by id')
results = c.fetchall()

for result in results:
     req.write ('%s' %result[0])
     req.write (',')
     req.write ('%s' %result[1])
     req.write ('<br>')


db.close()



%>
</body>
</html>


作者: crykun    時間: 2012-04-19 17:46
問題解決了,自問自答吧,字符集是個很折騰人的事。如果有人遇到了,也好有個參考。

文件里前面寫上:

os.environ['NLS_LANG'] = 'SIMPLIFIED CHINESE_CHINA.UTF8'
req.content_type='text/xml'
req.charset = 'utf-8'
req.send_http_header()

下面再寫req.write(string)就是你的頁面格式,編碼和頁面都正確。



如果是html,則第二行寫成 req.content_type='text/html'   即可。




歡迎光臨 Chinaunix (http://www.72891.cn/) Powered by Discuz! X3.2