Python + apache Server SETUP

Source: Internet
Author: User

Recently, I used Python + Apache to build a server and found a problem: for the Content-Type of POST request + application/octet-stream, the server always returns the 501 error.

Later, I checked the mod_python/util. py script and found that the configured pythonhandler is mod_python.publisher. For post requests, its content-type has constraints:

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 indicates that the Server Returns Error 501.

It seems that mod_python.publisher cannot be used as pythonhandler!

Therefore, using a Custom Handler as the pythonhandler encountered another problem:

All requests are processed using this handler. As a result, different Python scripts cannot be used to serve different requests.

Think about it and make a roundabout way: use this handler for general control. Each different request dynamically sets its pythonhandler according to the URI.

The index. py script is as follows:

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;

In this way, with the following configuration file, everything will be 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>

I have mentioned the problem of using the logging module in Apache + python in another blog:

Http://blog.csdn.net/hqin6/article/details/6729341

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.