Python : Creating a socket in a thread
<p>I am trying to create a socket <strong>IN A THREAD</strong>, but i am unable to do so.
My code is</p>

<pre><code>#!/usr/bin/env python 

import threading
import socket

class test1():
    def serve(self):
        host = ''
        port = 9999
        th_obj = threading.Thread(target = self.thread_method, args = (host,port))
        th_obj.daemon = True
        th_obj.start()

    def thread_method(self,host,port):
        sock = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
        sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR,1)
        sock.bind(('',9999))
        sock.listen(5)
        while True:
            connection, addr = sock.accept()
            connection.settimeout(60)
            while True:
                data = connection.recv(2048)
                t = open('file.txt', 'a')
                t.write(data)

test1().serve()
</code></pre>

<p>The problem is the script won't when I set <code>daemon  = True</code> while it is working perfectly without it.</p>

<p>How can i solve this problem. It is essential to me to demonize it.</p>