TypeError: expected string byte value sequence, str type value found

I am trying to run a simple hello world application using mod_wsgifor Python 3. I am using Fedora 23. Here is my Apache virtual host configuration:

<VirtualHost *:80>
    ServerName localhost
    ServerAdmin admin@localhost
    # ServerAlias foo.localhost
    WSGIScriptAlias /headers /home/httpd/localhost/python/headers/wsgi.py
    DocumentRoot /home/httpd/localhost/public_html
    ErrorLog /home/httpd/localhost/error.log
    CustomLog /home/httpd/localhost/requests.log combined
</VirtualHost>

wsgi.py:

def application(environ, start_response):
    status = '200 OK'
    output = 'Hello World!'

    response_headers = [('Content-Type', 'text/plain'),
                        ('Content-Length', str(len(output)))]

    start_response(status, response_headers)

    return [output]

It works fine if I use mod_wsgiPython 2 ( sudo dnf remove python3-mod_wsgi -y && sudo dnf install mod_wsgi -y && sudo apachectl restart), but when using Python 3. I get an internal server 500 error. Here's the error log:

mod_wsgi (pid=899): Exception occurred processing WSGI script '/home/httpd/localhost/python/headers/wsgi.py'.
TypeError: sequence of byte string values expected, value of type str found

Update

Using encode()(or encode('utf-8')) on str(len(output))does not work either. Now I get:

Traceback (most recent call last):
  File "/home/httpd/localhost/python/headers/wsgi.py", line 8, in application
    start_response(status, response_headers)
TypeError: expected unicode object, value of type bytes found
+4
source share
1 answer

-, output , unicode. response_headers, output ( str(len(output)).encode('utf-8') 6 , ).

, :

def application(environ, start_response):
    status = '200 OK'
    output = b'Hello World!'

    response_headers = [('Content-type', 'text/plain'),
                        ('Content-Length', str(len(output)))]
    start_response(status, response_headers)

    return [output]

( mod_wsgi, .)

+14

Source: https://habr.com/ru/post/1624653/


All Articles