首页 文章

Apache2 whith mod_wsgi python3 'TypeError:'并返回错误500

提问于
浏览
0

我的'controller.py'脚本

#!/usr/bin/env python
# -*- coding: utf-8 -*-
import os

def application(environ, start_response):
    # the ouput string to respuesta var
    respuesta = "<p>Página web construida con <strong>Python!!!</strong></p>"
    # generating the response OK
    start_response('200 OK', [('Content-Type', 'text/html; charset=utf-8')])
    return respuesta

在'error.log'中:

[Mon Mar 13 12:36:32.656669 2017] [wsgi:error] [pid 28767:tid 139926041507584] [client 127.0.0.1:56382] mod_wsgi(pid = 28767):处理WSGI脚本'/ var / www /时发生异常蟒/应用/ controller.py” . [Mon Mar 13 12:36:32.656761 2017] [wsgi:error] [pid 28767:tid 139926041507584] [client 127.0.0.1:56382] TypeError:期望的字节字符串值序列,str类型的值找到ubuntu @ ip-- - :/ var / www / python / logs $ TypeError:期望的字节字符串值序列,找到str类型的值

我读过this questions但答案不起作用 .

我的site.conf

<VirtualHost *:80>

    ServerName app.salvaj.es
    ServerAdmin salvajgb@salvaj.es
    DocumentRoot /var/www/python/static
    WSGIScriptAlias / /var/www/python/app/controller.py

    ErrorLog /var/www/python/logs/error.log
    CustomLog /var/www/python/logs/access.log combined

    <Directory />
            Options FollowSymLinks
            AllowOverride None
    </Directory>
    <Directory /var/www/python/static>
            Options Indexes FollowSymLinks MultiViews
            AllowOverride None
            Order allow,deny
            allow from all
    </Directory>

</VirtualHost>

1 回答

  • 2

    你做错了两件事 .

    第一个是响应必须是可迭代的字节,而不是Unicode .

    第二个是你返回一个字符串,而不是一个字符串列表 . 后者使您的代码效率非常低,因为一次发回一个字符 .

    使用:

    return [respuesta.encode('UTF-8')]
    

    更好的是,不要自己从头开始编写WSGI应用程序,使用像Flask这样的Web框架,因为它可以为您处理所有这些类型的细节 .

相关问题