[Tutor] Python Daemons

Daniel Bosah dbosah at buffalo.edu
Tue Aug 1 14:48:19 EDT 2017


I'm following an online tutorial about threading. This is the code I've
used so far:

import socket
import threading
from queue import Queue



print_lock = threading.Lock()

target = 'pythonprogramming.net'

def portscan(port):
    s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    try:
       con = s.connect((target,port)) # con for connect
       with print_lock: # if sucessful run with statement
          print 'port', port, 'is open'
       con.close() # closes connection
    except:
           pass #if it doesn't work pass the method

def threader():
    while True:
        worker = q.get()
        portscan(worker)
        q.task_done
q = Queue()
for x in range(30):
    t = threading.Thread(target = threader() #creates a thread, gets
workers from q, set them to work on portscanning
    t.daemon() = True # want it to be a daemon
    t.start()
    #jobs = ports
for worker in range(1,101): # port zero invalid port
    q.put(worker) # puts worker to work
q.join() #waits till thread terminiates


    I don't know what a Daemon is, and I also don't know how to use it in
Python 2.7. Apparently its built in Python 3, but  I don't know how to use
it in Python 2.7. Any help would be appreciated.


More information about the Tutor mailing list