Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Close TCP port 80 and 443 after forking in Django

I am trying to fork() and exec() a new python script process from within a Django app that is running in apache2/WSGI Python. The new python process is daemonized so that it doesn't hold any association to apache2, but I know the HTTP ports are still open. The new process kills apache2, but as a result the new python process now holds port 80 and 443 open, and I don't want this.

How do I close port 80 and 443 from within the new python process? Is there a way to gain access to the socket handle descriptors so they can be closed?

like image 681
Matthew Miling Avatar asked Nov 04 '22 13:11

Matthew Miling


1 Answers

If you use the subprocess module to execute the script, the close_fds argument to the Popen constructor will probably do what you want:

If close_fds is true, all file descriptors except 0, 1 and 2 will be closed before the child process is executed.

Assuming they weren't simply closed, the first three file descriptors are traditionally stdin, stdout and stderr, so the listening sockets in the Django application will be among those closed in the child process.

like image 144
James Henstridge Avatar answered Nov 09 '22 06:11

James Henstridge