Trimax
Trimax

Reputation: 2473

Apache2 whith mod_wsgi python3 'TypeError:' and return error 500

My 'controller.py' script

#!/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

In '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 ubuntu@ip----:/var/www/python/logs$ TypeError: sequence of byte string values expected, value of type str found

I've read this questions but the answer doesn't works.

my 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>

Upvotes: 1

Views: 668

Answers (1)

Graham Dumpleton
Graham Dumpleton

Reputation: 58523

You are doing two things wrong.

The first is that the response must be an iterable over bytes, not Unicode.

The second is that you are returning a string, rather than a list of strings. The latter makes your code very inefficient as single character at a time is being sent back.

Use:

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

Better still, do not write WSGI applications from scratch yourself, use a web framework like Flask as it handles all these sorts of details for you.

Upvotes: 3

Related Questions