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>You could pass your counting number (<code>x</code>, in this case), as a variable in your send class. Keep in mind though that <code>x</code> will start at 0, not 1.</p>

<pre><code>for x in range(800)
    send(x+1).start()

class send(threading.Thread):
    def __init__(self, count):
        self.count = count

    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.count
            except:
                pass
</code></pre>

<p>Or, as Rob commented above in the other question, <a href="http://docs.python.org/library/threading.html#threading.current_thread" rel="nofollow"><code>threading.current_thread()</code></a> looks satisfactory.</p>

python multithreading sockets python-3.x