2017-03-13 31 views
0

我 'controller.py' 脚本的Apache2蒙山mod_wsgi的python3 '类型错误:' 并返回500错误

#!/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): Exception occurred processing WSGI script '/var/www/python/app/controller.py'. [Mon Mar 13 12:36:32.656761 2017]

[wsgi:error] [pid 28767:tid 139926041507584] [client 127.0.0.1:56382] TypeError: sequence of byte string values expected, value of type str found [email protected]---:/var/www/python/logs$ TypeError: sequence of byte string values expected, value of type str found

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

我site.conf

<VirtualHost *:80> 

    ServerName app.salvaj.es 
    ServerAdmin [email protected] 
    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> 
+0

有没有安装mod-wsgi? sudo aptitude install libapache2-mod-wsgi –

+0

@PierangeloOrizio是的,并启用'sudo a2enmod wsgi' – Trimax

+0

你可以发布虚拟主机配置吗? domain.conf –

回答

2

你是做两件事情是错误的。

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

第二个是你正在返回一个字符串,而不是一串字符串。后者会让您的代码非常低效,因为一次只能输入一个字符。

用途:

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

更妙的是,不要从头开始写自己的WSGI应用程序,它可以处理所有这些各种各样的细节为你使用像瓶Web框架。

+0

即可使用!是的,我会使用Flask,但首先我试图学习如何没有框架。 – Trimax

+0

您最好从Flask开始,只有在需要时才了解原始WSGI。原始的WSGI中有太多的陷阱,如果学习,它不是一个好的开始。 –