print current thread in python 3
<p>I have this script:</p>

<pre><code>import threading, socket

for x in range(800)
    send().start()

class send(threading.Thread):
    def run(self):
        while True:
            try:
                s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
                s.connect(("www.google.it", 80))
                s.send ("test")
                print ("Request sent!")
            except:
                pass
</code></pre>

<p>And at the place of "Request sent!" I would like to print something like: "Request sent! %s" % (the current number of the thread sending the request)</p>

<p>What's the fastest way to do it?</p>

<p><strong>--SOLVED--</strong></p>

<pre><code>import threading, socket

for x in range(800)
    send(x+1).start()

class send(threading.Thread):
    def __init__(self, counter):
        threading.Thread.__init__(self)
        self.counter = counter
    def run(self):
        while True:
            try:
                s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
                s.connect(("www.google.it", 80))
                s.send ("test")
                print ("Request sent! @", self.counter)
            except:
                pass
</code></pre>

<p>The easiest way to do this is to use <code>setName</code> and <code>getName</code> to give names to your threads.</p>

<pre><code>import threading, socket

for x in range(800)
    new_thread = send()
    new_thread.setName("thread number %d" % x)
    new_thread.start()

class send(threading.Thread):
    def run(self):
        while True:
            try:
                s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
                s.connect(("www.google.it", 80))
                s.send ("test")
                print ("Request sent by %s!" % self.getName())
            except:
                pass
</code></pre>

<p>You can also add any other attributes to <code>send</code> that you need to keep track of your threads.</p>

python multithreading sockets python-3.x