python+apache的服务器搭建问题
来源:互联网 发布:知乎app ios7 下载 编辑:程序博客网 时间:2024/05/22 15:03
最近使用python+apache搭建服务器,发现一个问题:对于POST请求+application/octet-stream的Content-Type,服务器总是返回501错误。
后来查看了mod_python/util.py脚本,发现设置的PythonHandler是mod_python.publisher,对于POST请求,其Content-Type有约束条件:
109 if req.method != "POST":110 return111 112 try:113 clen = int(req.headers_in["content-length"])114 except (KeyError, ValueError):115 # absent content-length is not acceptable116 raise apache.SERVER_RETURN, apache.HTTP_LENGTH_REQUIRED117 118 if not req.headers_in.has_key("content-type"):119 ctype = "application/x-www-form-urlencoded"120 else:121 ctype = req.headers_in["content-type"]122 123 if ctype.startswith("application/x-www-form-urlencoded"):124 pairs = parse_qsl(req.read(clen), keep_blank_values)125 for pair in pairs:126 # TODO : isn't this a bit heavyweight just for form fields ?127 file = cStringIO.StringIO(pair[1])128 self.list.append(Field(pair[0], file, "text/plain", {}, None, {}))129 return130 131 if not ctype.startswith("multipart/"):132 # we don't understand this content-type133 raise apache.SERVER_RETURN, apache.HTTP_NOT_IMPLEMENTED
其中的HTTP_NOT_IMPLEMENTED就是服务器返回的501错误。
看来不能采用mod_python.publisher来作为PythonHandler了!
于是使用自定义的handler来作为PythonHandler,遇到另一个问题:
任何的请求都会使用这个handler来处理,导致不能使用多个不同的python脚本服务不同的请求。
思来想去,做了个迂回的方式:使用这个handler来做总控,每个不同的请求根据uri动态设置其PythonHandler
index.py脚本如下:
import os;from mod_python import apachedef handler(req):handler = req.uri[1:];if handler[-3:] == ".py" :handler = handler[0:-3];if not handler == "index" :req.add_handler("PythonHandler", handler);return apache.OK;
这样配合下面的配置文件,一切就都ok了!
AddDefaultCharset GBKListen 8080<VirtualHost *:8080># AddType application/x-http-python .py DocumentRoot /usr/htdocs <IfModule alias_module> Alias /index /usr/htdocs/index.py </IfModule> <Directory "/usr/htdocs"> AllowOverride FileInfo SetHandler mod_python #PythonHandler mod_python.publisher PythonHandler index PythonDebug On Order allow,deny Allow from all </Directory></VirtualHost>
关于apache+python中使用logging模块的问题,在我另一篇博客中有提及:
http://blog.csdn.net/hqin6/article/details/6729341
- python+apache的服务器搭建问题
- Mac Python服务器搭建遇到的问题
- apache服务器搭建过程中需要注意到的一些问题
- 搭建Apache+web.py+mod_wsgi python服务器
- Apache http 服务器的搭建
- 服务器的搭建tomcat+apache
- 搭建Apache服务器的虚拟主机
- python 服务器的架构搭建
- Subversion+Apache的源码服务器搭建
- Apache服务器的搭建,尚观Day13
- Apache James 搭建自己的邮件服务器
- 搭建胜过Apache十倍的Web服务器
- linux学习之一 apache服务器的搭建
- 使用apache的ftpserver搭建ftp服务器
- 关于apache搭建PC服务器的若干问题
- linux、apache、svn服务器的搭建
- Apache James 搭建自己的邮件服务器
- 苹果的web服务器(Apache)搭建
- Digital Roots
- 二叉树中两个子节点的最近公共节点
- 网站建站教程 | HTML教程 | CSS教程 | JavaScript教程
- QString -> char *
- iOS 从图片截取图片,类似Window bitblt
- python+apache的服务器搭建问题
- 普通人的编辑利器——Vim
- linux 下 java 安装 环境变量设置
- cannot restore segment prot after reloc: Permission denied
- SERVLET配置到WEB.XML中
- 婚纱照
- [PATCH] input: add driver for Bosch Sensortec's BMA150 accelerometer
- java.lang.OutOfMemoryError: PermGen space及其解决方法
- 2011年8月25日阅读文档vim/gvim, ue相关