import socket import subprocess import os """ When a suprocess.Popen command is issued while a socket connection is being handled the socket connection will not close until the output of the subprocess is consumed. The connection remains open even though the request.close() command returns successfully and we start listening for the next connection. This problem does not show up with the equivalent os.popen command. Replace the command being executed with your favorite program that produces a significant amount of output. To reproduce: run socketTest.py use telnet to connect on port 6288 watch for connection to close or not """ sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) sock.bind(('',6288)) sock.listen(5) while 1: request, client_addr = sock.accept() #os.popen('C:\\WINDOWS\\system32\\cmd.exe /c type C:\\WINDOWS\\system32\\mqperf.ini') subprocess.Popen('C:\\WINDOWS\\system32\\cmd.exe /c type C:\\WINDOWS\\system32\\mqperf.ini', stdout=subprocess.PIPE) request.close()