[modwsgi] Permission denied when trying to create a simple TCP server

218 views
Skip to first unread message

Kaktus621

unread,
Dec 25, 2009, 4:47:09 AM12/25/09
to modwsgi
Hello,

some days ago i have isntalled the wsgi module into my apache and the
simple Hello, World application runs fine. The next step I did want to
do is to make a TCP server that is listening on a certain port (e.g.
621). Because of this, I modified the Hello World application to the
following code:

######

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]

from wsgiref.simple_server import make_server
srv = make_server('localhost', 621, application)
srv.serve_forever()

######

But now the script won't run anymore, here is the error.log of the
apache:

######

[Thu Dec 24 19:38:19 2009] [error] [client 192.168.178.2] mod_wsgi
(pid=5612): Target WSGI script '/var/www/python/test.wsgi' cannot be
loaded as Python module.
[Thu Dec 24 19:38:19 2009] [error] [client 192.168.178.2] mod_wsgi
(pid=5612): Exception occurred processing WSGI script '/var/www/python/
test.wsgi'.
[Thu Dec 24 19:38:19 2009] [error] [client 192.168.178.2] Traceback
(most recent call last):
[Thu Dec 24 19:38:19 2009] [error] [client 192.168.178.2] File "/var/
www/python/test.wsgi", line 12, in <module>
[Thu Dec 24 19:38:19 2009] [error] [client 192.168.178.2] srv =
make_server('localhost', 621, application)
[Thu Dec 24 19:38:19 2009] [error] [client 192.168.178.2] File "/usr/
lib/python2.6/wsgiref/simple_server.py", line 181, in make_server
[Thu Dec 24 19:38:19 2009] [error] [client 192.168.178.2] server =
server_class((host, port), handler_class)
[Thu Dec 24 19:38:19 2009] [error] [client 192.168.178.2] File "/usr/
lib/python2.6/SocketServer.py", line 400, in __init__
[Thu Dec 24 19:38:19 2009] [error] [client 192.168.178.2]
self.server_bind()
[Thu Dec 24 19:38:19 2009] [error] [client 192.168.178.2] File "/usr/
lib/python2.6/wsgiref/simple_server.py", line 50, in server_bind
[Thu Dec 24 19:38:19 2009] [error] [client 192.168.178.2]
HTTPServer.server_bind(self)
[Thu Dec 24 19:38:19 2009] [error] [client 192.168.178.2] File "/usr/
lib/python2.6/BaseHTTPServer.py", line 108, in server_bind
[Thu Dec 24 19:38:19 2009] [error] [client 192.168.178.2]
SocketServer.TCPServer.server_bind(self)
[Thu Dec 24 19:38:19 2009] [error] [client 192.168.178.2] File "/usr/
lib/python2.6/SocketServer.py", line 411, in server_bind
[Thu Dec 24 19:38:19 2009] [error] [client 192.168.178.2]
self.socket.bind(self.server_address)
[Thu Dec 24 19:38:19 2009] [error] [client 192.168.178.2] File
"<string>", line 1, in bind
[Thu Dec 24 19:38:19 2009] [error] [client 192.168.178.2] error:
[Errno 13] Permission denied

######

Do you know why this is happening? Is there another way to create such
a server or to grant the module to create sockets? Thank you in advance

Graham Dumpleton

unread,
Dec 25, 2009, 5:20:06 AM12/25/09
to mod...@googlegroups.com
You cant do that for a couple of reasons.

The first is that Apache/mod_wsgi is often multi process and so your
code would get executed in multiple process at same time. Since only
one process can acquire listener port, all but that one will fail.

Second, you can not run blocking operations like srv.serve_forever()
in global scope of WSGI script file import. You shouldn't even do that
specific one in raw request handler either. The only way you could do
something like that is to start a separate thread within the process
and do it within the thread. That way you will not block the request
handler thread.

Even if you do the latter, you will still have multi process issue.
Only solution to that is use Windows, or if on UNIX use daemon mode
and delegate application to run in a single process daemon process
group.

Graham

2009/12/25 Kaktus621 <k6...@arcor.de>:

> --
>
> You received this message because you are subscribed to the Google Groups "modwsgi" group.
> To post to this group, send email to mod...@googlegroups.com.
> To unsubscribe from this group, send email to modwsgi+u...@googlegroups.com.
> For more options, visit this group at http://groups.google.com/group/modwsgi?hl=en.
>
>
>

Kaktus621

unread,
Dec 25, 2009, 8:00:59 AM12/25/09
to modwsgi
Ah good to know about that. I managed to open a server by simply
calling the Script with python (i.e. "sudo python file.wsgi") - With
that I could access the application through port 621 of my server
(After changing 'localhost' to '' in order to make it public
accessible).

On 25 Dez., 11:20, Graham Dumpleton <graham.dumple...@gmail.com>
wrote:


> You cant do that for a couple of reasons.
>
> The first is that Apache/mod_wsgi is often multi process and so your
> code would get executed in multiple process at same time. Since only
> one process can acquire listener port, all but that one will fail.
>
> Second, you can not run blocking operations like srv.serve_forever()
> in global scope of WSGI script file import. You shouldn't even do that
> specific one in raw request handler either. The only way you could do
> something like that is to start a separate thread within the process
> and do it within the thread. That way you will not block the request
> handler thread.
>
> Even if you do the latter, you will still have multi process issue.
> Only solution to that is use Windows, or if on UNIX use daemon mode
> and delegate application to run in a single process daemon process
> group.
>
> Graham
>

> 2009/12/25 Kaktus621 <k...@arcor.de>:

Damjan

unread,
Dec 25, 2009, 10:11:28 AM12/25/09
to modwsgi
> Do you know why this is happening? Is there another way to create such
> a server or to grant the module to create sockets? Thank you in advance

Graham told you how this is a bad idea, but also...

the error you are getting is because you are trying to open a TCP port
below 1024 (621) which only root can open. Your mod_wsgi app doesn't
run with root privileges and it could never open a listening TCP
socket below 1024

Jason Garber

unread,
Dec 25, 2009, 8:16:51 PM12/25/09
to mod...@googlegroups.com
If you are starting your script as root, in order to bind to the port, make sure you are shedding root privileges after you have acquired any necessary file handles, etc...

Graham Dumpleton

unread,
Dec 26, 2009, 4:51:13 AM12/26/09
to mod...@googlegroups.com
2009/12/26 Damjan <gda...@gmail.com>:

Well caught. I didn't pay attention to the actual port number.

Graham

Reply all
Reply to author
Forward
0 new messages