【问题标题】:Apache2 whith mod_wsgi python3 'TypeError:' and return error 500Apache2 whith mod_wsgi python3 'TypeError:' 并返回错误 500
【发布时间】:2017-08-03 12:04:24
【问题描述】:

我的“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'中:

[2017 年 3 月 13 日星期一 12:36:32.656669] [wsgi:error] [pid 28767:tid 139926041507584] [客户端 127.0.0.1:56382] mod_wsgi (pid=28767): 处理 WSGI 脚本时发生异常 '/var/www/python/app/controller.py'。 [2017 年 3 月 13 日星期一 12:36:32.656761]

[wsgi:error] [pid 28767:tid 139926041507584] [client 127.0.0.1:56382] TypeError:预期的字节字符串值序列,str 类型的值 发现 ubuntu@ip----:/var/www/python/logs$ TypeError: 预期的字节字符串值序列,找到类型 str 的值

我已阅读this questions,但答案无效。

我的站点.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>

【问题讨论】:

  • 你安装了mod-wsgi吗? sudo aptitude install libapache2-mod-wsgi
  • @PierangeloOrizio 是的,并通过sudo a2enmod wsgi启用
  • 你能发布虚拟主机配置吗?域.conf
  • @PierangeloOrizio 完成! site.conf 添加了

标签: linux python-3.x ubuntu apache2 mod-wsgi


【解决方案1】:

你做错了两件事。

首先是响应必须是可迭代的字节,而不是 Unicode。

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

用途:

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

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

【讨论】:

  • 行得通!是的,我会使用 Flask,但首先我尝试学习如何在没有框架的情况下使用它。
  • 您最好从 Flask 开始,并且只有在需要时才了解原始 WSGI。原始 WSGI 中的陷阱太多,如果学习,这不是一个好的起点。
猜你喜欢
  • 2011-08-08
  • 1970-01-01
  • 2019-11-25
  • 2014-02-03
  • 1970-01-01
  • 1970-01-01
  • 2011-12-17
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多