[Tutor] Python Daemons
Steven D'Aprano
steve at pearwood.info
Tue Aug 1 20:49:09 EDT 2017
Hi Daniel,
My responses below.
On Tue, Aug 01, 2017 at 02:48:19PM -0400, Daniel Bosah wrote:
> I'm following an online tutorial about threading. This is the code I've
> used so far:
Can you give us a link to the tutorial?
[...]
> 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
I'm very concerned about that bare "except" line. I'm not absolutely
saying that it is wrong, but in general bare excepts are a terrible
idea.
https://realpython.com/blog/python/the-most-diabolical-python-antipattern/
> 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,
https://en.wikipedia.org/wiki/Daemon_(computing)
> 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.
What happens when you try?
--
Steve
More information about the Tutor
mailing list