Problem with wsgihandler.py Apache/Python3

820 views
Skip to first unread message

motionbutterfly

unread,
Sep 2, 2018, 6:03:30 PM9/2/18
to web2py-users
Hello, I was wondering if anyone here could help me. I've been migrating a web2py application from Python2.7 to Python3.4, this application was already being hosted in a webserver with Apache2.4 following the steps in the Deployment Recipes chapter of the web2py book. I reconfigured apache to run with python 3, and reinstalled mod_wsgi using pip. My problem is that as soon as I load my application I get an internal error, and when I click on the ticket it shows me "500 Internal Server Error", and when I look up the error log for apache this is what comes up: 

[Thu Aug 23 22:42:17.748139 2018] [wsgi:warn] [pid 2268:tid 139927378876288] mod_wsgi: Compiled for Python/3.4.2rc1+.
[Thu Aug 23 22:42:17.748154 2018] [wsgi:warn] [pid 2268:tid 139927378876288] mod_wsgi: Runtime using Python/3.4.2.
[Thu Aug 23 22:42:17.896498 2018] [mpm_event:notice] [pid 2268:tid 139927378876288] AH00489: Apache/2.4.10 (Debian) OpenSSL/1.0.1t mod_wsgi/4.3.0 Python/3.4.2 mod_perl/2.0.9dev Perl/v5.20.2 configured -- resuming normal operations
[Thu Aug 23 22:42:17.896584 2018] [core:notice] [pid 2268:tid 139927378876288] AH00094: Command line: '/usr/sbin/apache2'
[Thu Aug 23 22:45:29.528373 2018] [wsgi:error] [pid 2272:tid 139927187830528] <class 'int'>
[Thu Aug 23 22:45:29.528468 2018] [wsgi:error] [pid 2272:tid 139927187830528] 10
[Thu Aug 23 22:45:29.968522 2018] [wsgi:error] [pid 2272:tid 139927213033216] Loads pas\xc3\xb3
[Thu Aug 23 22:45:35.289112 2018] [wsgi:error] [pid 2272:tid 139927179421440] [remote 186.89.23.145:25178] mod_wsgi (pid=2272): Exception occurred processing WSGI script '/home/www-data/web2py/wsgihandler.py'.
[Thu Aug 23 22:45:35.289293 2018] [wsgi:error] [pid 2272:tid 139927179421440] [remote 186.89.23.145:25178] TypeError: sequence of byte string values expected, value of type str found

Which seems to suggest that the problem is inside the wsgihandler.py script, but doesn't point out where. Has this happened to anyone else before?

Thanks in advance for any help.

黄祥

unread,
Sep 3, 2018, 12:28:05 AM9/3/18
to web2py-users
there are a lot of suggestion in this forum to not use apache to serve web2py, perhaps you can try another web server, e.g. nginx, gunicorn
wsgihandler.py had tested and running fine with gunicorn

best regards,
stifan

Peter

unread,
Sep 8, 2018, 7:22:49 PM9/8/18
to web...@googlegroups.com
Try to remove all session files. I think that problem is in pickle which is different in python 2 and python 3 (data type is not same in python 2 and python 3).

Use fresh source code of web2py with your application. Don't start web2py with python 3 after use with python 2. 

Dave

unread,
Nov 18, 2018, 6:46:35 PM11/18/18
to web2py-users
I'm having the same troubles after upgrading to python3 using Apache with libapache2-mod-wsgi-py3 4.5.17-1 on Ubuntu.

I did delete all session files, and I am using fresh source code of web2py (R-2.17.2).

I'm running several applications in web2py, and when i restart Apache they work for a while. Then at some point later, and I haven't figured out what initiates the problem, I am getting:

Traceback (most recent call last):
  File "/home/ubuntu/mnt/web2py/gluon/main.py", line 445, in wsgibase
    serve_controller(request, response, session)
  File "/home/ubuntu/mnt/web2py/gluon/main.py", line 207, in serve_controller
    raise HTTP(response.status, page, **response.headers)
gluon.http.HTTP: 200 OK
 
During handling of the above exception, another exception occurred:
 
Traceback (most recent call last):
  File "/home/ubuntu/mnt/web2py/gluon/main.py", line 472, in wsgibase
    BaseAdapter.close_all_instances('commit')
  File "/home/ubuntu/mnt/web2py/gluon/packages/dal/pydal/connection.py", line 121, in close_all_instances
    db._adapter.close(action)
  File "/home/ubuntu/mnt/web2py/gluon/packages/dal/pydal/connection.py", line 98, in close
    if len(pool) < self.pool_size:
TypeError: '<' not supported between instances of 'int' and 'str'


In my apache error log I see this:

[Thu Nov 15 04:02:26.546712 2018] [wsgi:error] [pid 9117] [client XXX.XXX.XXX.AA:56599] Timeout when reading response headers from daemon process 'web2pydefault': /home/ubuntu/mnt/web2py/wsgihandler.py
[Thu Nov 15 04:08:52.161152 2018] [wsgi:error] [pid 9111] [client XXX.XXX.XXX.AB:40713] Timeout when reading response headers from daemon process 'web2pydefault': /home/ubuntu/mnt/web2py/wsgihandler.py
[Thu Nov 15 04:26:51.746340 2018] [wsgi:error] [pid 8081] [client XXX.XXX.XXX.AC:2960] Timeout when reading response headers from daemon process 'web2pydefault': /home/ubuntu/mnt/web2py/wsgihandler.py
[Thu Nov 15 04:38:41.429260 2018] [wsgi:error] [pid 8081] [client XXX.XXX.XXX.AD:48360] Timeout when reading response headers from daemon process 'web2pydefault': /home/ubuntu/mnt/web2py/wsgihandler.py
[Thu Nov 15 04:44:56.502509 2018] [wsgi:error] [pid 7466] [remote XXX.XXX.XXX.AE:7339] mod_wsgi (pid=7466): Exception occurred processing WSGI script '/home/ubuntu/mnt/web2py/wsgihandler.py'.
[Thu Nov 15 04:44:56.502575 2018] [wsgi:error] [pid 7466] [remote XXX.XXX.XXX.AE:7339] TypeError: sequence of byte string values expected, value of type str found
[Thu Nov 15 04:50:20.030220 2018] [wsgi:error] [pid 8081] [client XXX.XXX.XXX.AF:33766] Timeout when reading response headers from daemon process 'web2pydefault': /home/ubuntu/mnt/web2py/wsgihandler.py
[Thu Nov 15 05:00:40.384970 2018] [ssl:warn] [pid 9665] AH01909: XXX.XXX.XXX.AG:443:0 server certificate does NOT include an ID which matches the server name
[Thu Nov 15 05:00:40.407686 2018] [ssl:warn] [pid 9667] AH01909: XXX.XXX.XXX.AG:443:0 server certificate does NOT include an ID which matches the server name
[Thu Nov 15 05:03:10.875058 2018] [wsgi:error] [pid 9668] [remote XXX.XXX.XXX.AF:34126] /home/ubuntu/mnt/web2py/gluon/contrib/pymysql/cursors.py:166: Warning: (3090, "Changing sql mode 'NO_AUTO_CREATE_USER' is deprecated. It will be removed in a future release.")
[Thu Nov 15 05:03:10.875093 2018] [wsgi:error] [pid 9668] [remote XXX.XXX.XXX.AF:34126]   result = self._query(query)

Is it possibly related to this?:

Peter

unread,
Nov 20, 2018, 1:59:34 PM11/20/18
to web2py-users
Check if you have in your application in file appadmin.py code:
try:
  ...
except Exception, e:
  ...

except Exception, e: is python 2 and should be changed to:

except Exception as e:

Carl Petersen

unread,
Dec 7, 2018, 1:50:16 PM12/7/18
to web2py-users
Hello,

I'm having a similar issue.  I'm using Python 3.6.6, mod_wsgi 4.6.5, Apache 2.4.6 with web2py 2.17.2.

I can get to the welcome screen, but as soon as I try to access the admin application I get the following on my screen:

Internal Server Error

The server encountered an internal error or misconfiguration and was unable to complete your request.

Please contact the server administrator at root@localhost to inform them of the time this error occurred, and the actions you performed just before this error.

More information about this error may be available in the server error log.


And this in my apache error_log:

 [Fri Dec 07 08:32:14.626246 2018] [mpm_prefork:notice] [pid 30005] AH00163: Apache/2.4.6 (Red Hat Enterprise Linux) OpenSSL/1.0.2k-fips mod_wsgi/4.6.5 Python/3.6 configured -- resuming normal operations

[Fri Dec 07 08:32:33.572686 2018] [wsgi:error] [pid 30007] [remote 10.20.100.133:56416] mod_wsgi (pid=30007): Exception occurred processing WSGI script '/opt/web-apps/web2py/wsgihandler.py'.

[Fri Dec 07 08:32:33.572836 2018] [wsgi:error] [pid 30007] [remote 10.20.100.133:56416] TypeError: sequence of byte string values expected, value of type str found


Any assistance would be greatly appreciated.


Carl Petersen

unread,
Dec 7, 2018, 3:47:00 PM12/7/18
to web2py-users
I was able to get web2py up and running by changing the following line (131) in http.py  I don't expect that this was the proper way to do it (especially considering backward compatibility), but at lease it might point out where the issue is:

    elif isinstance(body, (str, bytes, bytearray)):
         if isinstance(body, unicodeT):
             body = to_bytes(body)
         return [body]
    elif hasattr(body, '__iter__'):
        return [x.encode('utf-8') for x in body]  ----- Changed from  "return body"
    else:
        body = str(body)
        if isinstance(body, unicodeT):
            body = to_bytes(body)
        return [body]
 
I didn't know whether I should just apply to_bytes(body) like the other returns because the other returns were coded "return [body]" instead of "return body".

Thanks

Peter

unread,
Dec 8, 2018, 10:53:05 AM12/8/18
to web2py-users
Similar situation can happen when you try to access admin page through simple http protocol over network. Web2py will raise exception and we got that error:

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

Code responsible for that error is:

if request.is_https:
    session.secure()
elif (remote_addr not in hosts) and (remote_addr != "127.0.0.1") and \
    (request.function != 'manage'):
    raise HTTP(200, T('appadmin is disabled because insecure channel'))

One solution is to erase all of this and just put session.secure() but this is not good idea and can be use only for testing.

Source of problem is combination of T() and HTTP() helper. In production I just remove T() helper like this:

raise HTTP(200, 'appadmin is disabled because insecure channel')

and that solve my problem. Maybe someone have beter solution for that problem?

Of course, this will happen in all situation when web2py raise HTTP() with T() as argument. So to avoid that I just use string as argument without T() helper.

Peter

unread,
Dec 8, 2018, 1:56:30 PM12/8/18
to web2py-users
In my environment this work. Of course, after that you should enable admin through http or configure https. This correction only enable to see what is problem.

web2py/applications/admin/models/access.py


# original code
elif not request.is_local and not DEMO_MODE:
    raise HTTP(200, T('Admin is disabled because insecure channel'))


# corrected
elif not request.is_local and not DEMO_MODE:
    raise HTTP(200, 'Admin is disabled because insecure channel')



On Friday, December 7, 2018 at 2:50:16 PM UTC+1, Carl Petersen wrote:

Massimo Di Pierro

unread,
Dec 30, 2018, 9:49:19 PM12/30/18
to web2py-users
This looks correct. Can you please submit a PR?

Andrew Rogers

unread,
Nov 6, 2020, 8:21:07 AM11/6/20
to web2py-users
This still seems like a problem. In my case I had forgotten to create a password after installing web2py on Debian. I assume it was throwing an error on:

raise HTTP(200, T('admin disabled because no admin password'))

After I created a password this error went away:

Internal Server Error
The server encountered an internal error or misconfiguration and was unable to complete your request.

Configuring a password is done by running the following code from the web2py folder.

sudo -u www-data python -c "from gluon.main import save_password; save_password(input('admin password: '), 443)"
Reply all
Reply to author
Forward
0 new messages