apache运行webpy项目遇到换行符无故增加的恼人问题,apachewebpy,py文件如下:#!C:\


py文件如下:

#!C:\Python26\python.exe#coding:utf-8import webweb.config.debug = True        urls = (    '/.*', 'hello')app = web.application(urls, globals())render_test=web.template.render('templates/')class hello:            def GET(self):        return render_test.test("")    def POST(self):        query = web.input()        return render_test.test(query.content)if __name__ == "__main__":    app.run()

模版文件:

$def with(content)<!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" xml:lang="en"><head><meta http-equiv="Content-Type" content="text/html;charset=UTF-8" /><title>test</title></head><body><form action="" method="post"><textarea name="content" id="" cols="30" rows="10">$content</textarea><input type="submit" value="post" /></form></body></html>



以上大家看到的再现这个问题的小程序,我向当前页面提交一个两行的文本,提交后,这个换行就会成倍增加,一个变两个,两个会成四个,但这只是在apache中是这样(别的机子还没有测试过),但如果直接运行webpy自己的服务来显示页面则没有这个问题


上图为在apache中的运行的结果

上图为直接运行py文件的结果

从两个图上看是有点区别的,但这个区别不知道说明什么问题?

apache版本 2.0 设置如下:

Listen 8820<VirtualHost   test.com><Directory "E:\py">AllowOverride AllOptions FollowSymLinksOrder allow,denyAllow from all</Directory>ServerAdmin   hellolibo@gmail.comDocumentRoot   E:\pyblogServerName   blog  ErrorLog   logs/aa_logCustomLog   logs/aa-access_log   common      LogLevel info</VirtualHost>

.htaccess文件如下:

Options +ExecCGI AddHandler cgi-script .py<IfModule mod_rewrite.c>        RewriteEngine on  RewriteBase /  RewriteRule ^static/(.*)$  static/$1 [L]  RewriteRule ^upfile/(.*)$  upfile/$1 [L]  RewriteCond %{REQUEST_URI} !^(/.*)+t.py/  RewriteRule ^(.*)$ t.py/$1 [PT]</IfModule>

------------------------------------------------------------------------------------------
现在我如果把接收到的数据做如下操作:

query.content.replace("\n","")

或者
query.content.replace("\r","")

都可以避免之前的情况

如果我没猜错的话,这是一个只会在windows平台上遇到的问题,因为只有windows平台的换行符才是\r\n,似乎在输出的时候会把换行符从\n变成\r\n,因此你会在编辑框里看到两个换行。暂时不知道这个问题产生的具体原因,但你可以试试下面的方法,把脚本的第一行改成

#!C:\Python26\python.exe -u
-u的解释是
Unbuffered binary stdout and stderr (also PYTHONUNBUFFERED=x).

如果能解决你的问题,你可以测试下linux下面的情况,如果猜测正确,linux应该不会出现你说的这个问题

编橙之家文章,

评论关闭