SlideShare a Scribd company logo
1 of 15
Python for Ethical Hackers
Mohammad reza Kamalifard
Kamalifard@datasec.ir
Python Language Essentials
Part 5 :
Multi-threaded Programming and Queue
Mohammad reza Kamalifard
Kamalifard.ir/pysec101
Thread
Running several threads is similar to running several different
programs concurrently, but with the following benefits:
Multiple threads within a process share the same data space with the
main thread and can therefore share information or communicate
with each other more easily than if they were separate processes.
Threads sometimes called light-weight processes and they do not
require much memory overhead; they care
cheaper than processes.
Mohammad reza Kamalifard
Kamalifard.ir/pysec101
Start a newThread
Starting a New Thread:
To spawn another thread, you need to call following method available
in thread module:
thread.start_new_thread ( function, args[, kwargs] )
This method call enables a fast and efficient way to create new
threads in both Linux and Windows.
The method call returns immediately and the child thread starts and
calls function with the passed list of agrs. When function returns, the
thread terminates.
Here, args is a tuple of arguments; use an empty tuple to call function
without passing any arguments. kwargs is an optional dictionary of
keyword arguments.
Mohammad reza Kamalifard
Kamalifard.ir/pysec101
Start a newThread
import thread
import time
# Define a function for the thread
def print_time( threadName, delay):
count = 0
while count < 5:
time.sleep(delay)
count += 1
print "%s: %s" % ( threadName, time.ctime(time.time()) )
# Create two threads as follows
thread.start_new_thread( print_time, ("Thread-1", 2, ) )
thread.start_new_thread( print_time, ("Thread-2", 4, ) )
while True:
pass Mohammad reza Kamalifard
Kamalifard.ir/pysec101
The Threading Module
The newer threading module included with Python 2.4 provides much
more powerful, high-level support for threads than the thread
module discussed in the previous section.
The threading module exposes all the methods of the thread module
and provides some additional methods:
threading.activeCount(): Returns the number of thread objects that
are active.
threading.currentThread(): Returns the number of thread objects in
the caller's thread control.
threading.enumerate(): Returns a list of all thread objects that are
currently active.
Mohammad reza Kamalifard
Kamalifard.ir/pysec101
The Threading Module
In addition to the methods, the threading module has the Thread
class that implements threading. The methods provided by the
Thread class are as follows:
run(): The run() method is the entry point for a thread.
start(): The start() method starts a thread by calling the run
method.
join([time]): The join() waits for threads to terminate.
isAlive(): The isAlive() method checks whether a thread is still
executing.
getName(): The getName() method returns the name of a thread.
setName(): The setName() method sets the name of a thread.
Mohammad reza Kamalifard
Kamalifard.ir/pysec101
Creating Thread using Threading Module
To implement a new thread using the threading module, you have to
do the following:
Define a new subclass of the Thread class.
Override the __init__(self [,args]) method to add additional
arguments.
Then, override the run(self [,args]) method to implement what the
thread should do when started.
Once you have created the new Thread subclass, you can create an
instance of it and then start a new thread by invoking the start(),
which will in turn call run() method.
Mohammad reza Kamalifard
Kamalifard.ir/pysec101
Multi-threaded Queue
The Queue module allows you to create a new queue object that can
hold a specific number of items. There are following methods to
control the Queue:
get(): The get() removes and returns an item from the queue.
put(): The put adds item to a queue.
qsize() : The qsize() returns the number of items that are currently
in the queue.
empty(): The empty( ) returns True if queue is empty; otherwise,
False.
full(): the full() returns True if queue is full; otherwise, False.
Mohammad reza Kamalifard
Kamalifard.ir/pysec101
Create task queues
Threads receive tasks
Threads complete tasks and inform the queue
All threads exit once queue is empty
finally when all task executed the program exits
Mohammad reza Kamalifard
Kamalifard.ir/pysec101
import threading
import Queue
import time
class WorkerThread(threading.Thread):
def __init__(self, queue):
threading.Thread.__init__(self)
self.queue = queue
def run(self):
print "In WorkerThread"
while True:
counter = self.queue.get()
print "Ordered to sleep for %d seconds" % counter
time.sleep(counter)
print "Finished sleeping for %d seconds" % counter
self.queue.task_done()
Mohammad reza Kamalifard
Kamalifard.ir/pysec101
queue = Queue.Queue()
for i in range(10):
print "Creating WorkerThread : % d" % i
worker = WorkerThread(queue)
worker.setDaemon(True)
worker.start()
print "WorkerThread %d Created" % i
for j in range(10):
queue.put(j)
queue.join()
print "All tasks over!"
Mohammad reza Kamalifard
Kamalifard.ir/pysec101
Creating WorkerThread : 0
WorkerThread 0 Created
Creating WorkerThread : 1
In WorkerThread
WorkerThread 1 Created
Creating WorkerThread : 2
In WorkerThread
WorkerThread 2 Created
Creating WorkerThread : 3
In WorkerThread
WorkerThread 3 Created
Creating WorkerThread : 4
In WorkerThread
WorkerThread 4 Created
Creating WorkerThread : 5
In WorkerThread
WorkerThread 5 Created
Creating WorkerThread : 6
In WorkerThread
Ordered to sleep for 0 seconds
Finished sleeping for 0 seconds
Ordered to sleep for 1 seconds
Ordered to sleep for 2 seconds
Ordered to sleep for 3 seconds
...
Mohammad reza Kamalifard
Kamalifard.ir/pysec101
Ordered to sleep for 4 seconds
Ordered to sleep for 5 seconds
Ordered to sleep for 6 seconds
Ordered to sleep for 7 seconds
Ordered to sleep for 8 seconds
Ordered to sleep for 9 seconds
In WorkerThread
Finished sleeping for 1 seconds
Finished sleeping for 2 seconds
Finished sleeping for 3 seconds
Finished sleeping for 4 seconds
Finished sleeping for 5 seconds
Finished sleeping for 6 seconds
Finished sleeping for 7 seconds
Finished sleeping for 8 seconds
Finished sleeping for 9 seconds
All tasks over!
Mohammad reza Kamalifard
Kamalifard.ir/pysec101
This work is licensed under the Creative Commons
Attribution-NoDerivs 3.0 Unported License.
To view a copy of this license, visit http://creativecommons.org/licenses/by-nd/3.0/
Copyright 2013 Mohammad reza Kamalifard.
All rights reserved.

More Related Content

What's hot

Multithreaded programming
Multithreaded programmingMultithreaded programming
Multithreaded programming
Sonam Sharma
 
Gevent what's the point
Gevent what's the pointGevent what's the point
Gevent what's the point
seanmcq
 

What's hot (20)

Java Concurrency, Memory Model, and Trends
Java Concurrency, Memory Model, and TrendsJava Concurrency, Memory Model, and Trends
Java Concurrency, Memory Model, and Trends
 
P4
P4P4
P4
 
concurrency
concurrencyconcurrency
concurrency
 
Advanced #3 threading
Advanced #3  threading Advanced #3  threading
Advanced #3 threading
 
Multithreading and concurrency in android
Multithreading and concurrency in androidMultithreading and concurrency in android
Multithreading and concurrency in android
 
JavaCro'15 - Spring @Async - Dragan Juričić
JavaCro'15 - Spring @Async - Dragan JuričićJavaCro'15 - Spring @Async - Dragan Juričić
JavaCro'15 - Spring @Async - Dragan Juričić
 
Fork Join (BeJUG 2012)
Fork Join (BeJUG 2012)Fork Join (BeJUG 2012)
Fork Join (BeJUG 2012)
 
Fork and join framework
Fork and join frameworkFork and join framework
Fork and join framework
 
Android concurrency
Android concurrencyAndroid concurrency
Android concurrency
 
Parallel streams in java 8
Parallel streams in java 8Parallel streams in java 8
Parallel streams in java 8
 
Fork/Join for Fun and Profit!
Fork/Join for Fun and Profit!Fork/Join for Fun and Profit!
Fork/Join for Fun and Profit!
 
AMC Minor Technical Issues
AMC Minor Technical IssuesAMC Minor Technical Issues
AMC Minor Technical Issues
 
Multithreaded programming
Multithreaded programmingMultithreaded programming
Multithreaded programming
 
Vapor – Swift is not only for iOS anymore
Vapor – Swift is not only for iOS anymoreVapor – Swift is not only for iOS anymore
Vapor – Swift is not only for iOS anymore
 
Java fork join
Java fork joinJava fork join
Java fork join
 
Fork Join
Fork JoinFork Join
Fork Join
 
Gevent what's the point
Gevent what's the pointGevent what's the point
Gevent what's the point
 
Java Concurrency in Practice
Java Concurrency in PracticeJava Concurrency in Practice
Java Concurrency in Practice
 
Understanding greenlet
Understanding greenletUnderstanding greenlet
Understanding greenlet
 
Python twisted
Python twistedPython twisted
Python twisted
 

Viewers also liked

اسلاید سوم جلسه هفتم کلاس پایتون برای هکرهای قانونی
اسلاید سوم جلسه هفتم کلاس پایتون برای هکرهای قانونیاسلاید سوم جلسه هفتم کلاس پایتون برای هکرهای قانونی
اسلاید سوم جلسه هفتم کلاس پایتون برای هکرهای قانونی
Mohammad Reza Kamalifard
 

Viewers also liked (6)

اسلاید ارائه سوم جلسه ۱۰ کلاس پایتون برای هکر های قانونی
اسلاید ارائه سوم جلسه ۱۰ کلاس پایتون برای هکر های قانونی اسلاید ارائه سوم جلسه ۱۰ کلاس پایتون برای هکر های قانونی
اسلاید ارائه سوم جلسه ۱۰ کلاس پایتون برای هکر های قانونی
 
جلسه پنجم پایتون برای هکر های قانونی دوره مقدماتی پاییز ۹۲- ارائه ۲
جلسه پنجم پایتون برای هکر های قانونی دوره مقدماتی پاییز ۹۲- ارائه ۲جلسه پنجم پایتون برای هکر های قانونی دوره مقدماتی پاییز ۹۲- ارائه ۲
جلسه پنجم پایتون برای هکر های قانونی دوره مقدماتی پاییز ۹۲- ارائه ۲
 
اسلاید دوم جلسه دوم کلاس پایتون برای هکرهای قانونی
اسلاید دوم جلسه دوم کلاس پایتون برای هکرهای قانونیاسلاید دوم جلسه دوم کلاس پایتون برای هکرهای قانونی
اسلاید دوم جلسه دوم کلاس پایتون برای هکرهای قانونی
 
اسلاید اول جلسه چهارم کلاس پایتون برای هکرهای قانونی
اسلاید اول جلسه چهارم کلاس پایتون برای هکرهای قانونیاسلاید اول جلسه چهارم کلاس پایتون برای هکرهای قانونی
اسلاید اول جلسه چهارم کلاس پایتون برای هکرهای قانونی
 
اسلاید سوم جلسه هفتم کلاس پایتون برای هکرهای قانونی
اسلاید سوم جلسه هفتم کلاس پایتون برای هکرهای قانونیاسلاید سوم جلسه هفتم کلاس پایتون برای هکرهای قانونی
اسلاید سوم جلسه هفتم کلاس پایتون برای هکرهای قانونی
 
جلسه هفتم پایتون برای هکر های قانونی دوره مقدماتی پاییز ۹۲
جلسه هفتم پایتون برای هکر های قانونی دوره مقدماتی پاییز ۹۲جلسه هفتم پایتون برای هکر های قانونی دوره مقدماتی پاییز ۹۲
جلسه هفتم پایتون برای هکر های قانونی دوره مقدماتی پاییز ۹۲
 

Similar to اسلاید ارائه اول جلسه ۱۰ کلاس پایتون برای هکر های قانونی

Python multithreading session 9 - shanmugam
Python multithreading session 9 - shanmugamPython multithreading session 9 - shanmugam
Python multithreading session 9 - shanmugam
Navaneethan Naveen
 
OOPS object oriented programming UNIT-4.pptx
OOPS object oriented programming UNIT-4.pptxOOPS object oriented programming UNIT-4.pptx
OOPS object oriented programming UNIT-4.pptx
Arulmozhivarman8
 
Multithreading
MultithreadingMultithreading
Multithreading
backdoor
 
Runnable interface.34
Runnable interface.34Runnable interface.34
Runnable interface.34
myrajendra
 
Threads and Synchronization in c#
Threads and Synchronization in c#Threads and Synchronization in c#
Threads and Synchronization in c#
Rizwan Ali
 

Similar to اسلاید ارائه اول جلسه ۱۰ کلاس پایتون برای هکر های قانونی (20)

Python multithreading
Python multithreadingPython multithreading
Python multithreading
 
Multi Threading
Multi ThreadingMulti Threading
Multi Threading
 
Thread model in java
Thread model in javaThread model in java
Thread model in java
 
Python multithreading session 9 - shanmugam
Python multithreading session 9 - shanmugamPython multithreading session 9 - shanmugam
Python multithreading session 9 - shanmugam
 
Generators & Decorators.pptx
Generators & Decorators.pptxGenerators & Decorators.pptx
Generators & Decorators.pptx
 
Intake 38 12
Intake 38 12Intake 38 12
Intake 38 12
 
Md09 multithreading
Md09 multithreadingMd09 multithreading
Md09 multithreading
 
OOPS object oriented programming UNIT-4.pptx
OOPS object oriented programming UNIT-4.pptxOOPS object oriented programming UNIT-4.pptx
OOPS object oriented programming UNIT-4.pptx
 
Multithreading
MultithreadingMultithreading
Multithreading
 
Chapter 5 - THREADING & REGULAR exp - MAULIK BORSANIYA
Chapter 5 - THREADING & REGULAR exp - MAULIK BORSANIYAChapter 5 - THREADING & REGULAR exp - MAULIK BORSANIYA
Chapter 5 - THREADING & REGULAR exp - MAULIK BORSANIYA
 
Session 7_MULTITHREADING in java example.ppt
Session 7_MULTITHREADING in java example.pptSession 7_MULTITHREADING in java example.ppt
Session 7_MULTITHREADING in java example.ppt
 
Session 7_MULTITHREADING in java example.ppt
Session 7_MULTITHREADING in java example.pptSession 7_MULTITHREADING in java example.ppt
Session 7_MULTITHREADING in java example.ppt
 
Threadnotes
ThreadnotesThreadnotes
Threadnotes
 
Multithreading Presentation
Multithreading PresentationMultithreading Presentation
Multithreading Presentation
 
Multithreading.pptx
Multithreading.pptxMultithreading.pptx
Multithreading.pptx
 
Class notes(week 9) on multithreading
Class notes(week 9) on multithreadingClass notes(week 9) on multithreading
Class notes(week 9) on multithreading
 
Runnable interface.34
Runnable interface.34Runnable interface.34
Runnable interface.34
 
Threads and Synchronization in c#
Threads and Synchronization in c#Threads and Synchronization in c#
Threads and Synchronization in c#
 
Multithreading.pptx
Multithreading.pptxMultithreading.pptx
Multithreading.pptx
 
Threads
ThreadsThreads
Threads
 

More from Mohammad Reza Kamalifard

اسلاید اول جلسه اول دوره پاییز کلاس پایتون برای هکرهای قانونی
اسلاید اول جلسه اول دوره پاییز کلاس پایتون برای هکرهای قانونیاسلاید اول جلسه اول دوره پاییز کلاس پایتون برای هکرهای قانونی
اسلاید اول جلسه اول دوره پاییز کلاس پایتون برای هکرهای قانونی
Mohammad Reza Kamalifard
 
اسلاید دوم جلسه هفتم کلاس پایتون برای هکرهای قانونی
اسلاید دوم جلسه هفتم کلاس پایتون برای هکرهای قانونیاسلاید دوم جلسه هفتم کلاس پایتون برای هکرهای قانونی
اسلاید دوم جلسه هفتم کلاس پایتون برای هکرهای قانونی
Mohammad Reza Kamalifard
 
اسلاید اول جلسه هفتم کلاس پایتون برای هکرهای قانونی
اسلاید اول جلسه هفتم کلاس پایتون برای هکرهای قانونیاسلاید اول جلسه هفتم کلاس پایتون برای هکرهای قانونی
اسلاید اول جلسه هفتم کلاس پایتون برای هکرهای قانونی
Mohammad Reza Kamalifard
 

More from Mohammad Reza Kamalifard (20)

Internet Age
Internet AgeInternet Age
Internet Age
 
Introduction to Flask Micro Framework
Introduction to Flask Micro FrameworkIntroduction to Flask Micro Framework
Introduction to Flask Micro Framework
 
Pycon - Python for ethical hackers
Pycon - Python for ethical hackers Pycon - Python for ethical hackers
Pycon - Python for ethical hackers
 
Tehlug 26 Nov 2013 Hackers,Cyberwarfare and Online privacy
Tehlug 26 Nov 2013 Hackers,Cyberwarfare and Online privacyTehlug 26 Nov 2013 Hackers,Cyberwarfare and Online privacy
Tehlug 26 Nov 2013 Hackers,Cyberwarfare and Online privacy
 
جلسه دوم پایتون برای هکر های قانونی دوره مقدماتی پاییز ۹۲
جلسه دوم پایتون برای هکر های قانونی دوره مقدماتی پاییز ۹۲جلسه دوم پایتون برای هکر های قانونی دوره مقدماتی پاییز ۹۲
جلسه دوم پایتون برای هکر های قانونی دوره مقدماتی پاییز ۹۲
 
جلسه سوم پایتون برای هکر های قانونی دوره مقدماتی پاییز ۹۲
جلسه سوم پایتون برای هکر های قانونی دوره مقدماتی پاییز ۹۲جلسه سوم پایتون برای هکر های قانونی دوره مقدماتی پاییز ۹۲
جلسه سوم پایتون برای هکر های قانونی دوره مقدماتی پاییز ۹۲
 
جلسه چهارم پایتون برای هکر های قانونی دوره مقدماتی پاییز ۹۲
جلسه چهارم پایتون برای هکر های قانونی دوره مقدماتی پاییز ۹۲جلسه چهارم پایتون برای هکر های قانونی دوره مقدماتی پاییز ۹۲
جلسه چهارم پایتون برای هکر های قانونی دوره مقدماتی پاییز ۹۲
 
جلسه پنجم پایتون برای هکر های قانونی دوره مقدماتی پاییز ۹۲- ارائه ۱
جلسه پنجم پایتون برای هکر های قانونی دوره مقدماتی پاییز ۹۲- ارائه ۱جلسه پنجم پایتون برای هکر های قانونی دوره مقدماتی پاییز ۹۲- ارائه ۱
جلسه پنجم پایتون برای هکر های قانونی دوره مقدماتی پاییز ۹۲- ارائه ۱
 
جلسه ششم پایتون برای هکر های قانونی دوره مقدماتی پاییز ۹۲- ارائه ۱
جلسه ششم پایتون برای هکر های قانونی دوره مقدماتی پاییز ۹۲- ارائه ۱جلسه ششم پایتون برای هکر های قانونی دوره مقدماتی پاییز ۹۲- ارائه ۱
جلسه ششم پایتون برای هکر های قانونی دوره مقدماتی پاییز ۹۲- ارائه ۱
 
جلسه ششم پایتون برای هکر های قانونی دوره مقدماتی پاییز ۹۲- ارائه ۲
جلسه ششم پایتون برای هکر های قانونی دوره مقدماتی پاییز ۹۲- ارائه ۲جلسه ششم پایتون برای هکر های قانونی دوره مقدماتی پاییز ۹۲- ارائه ۲
جلسه ششم پایتون برای هکر های قانونی دوره مقدماتی پاییز ۹۲- ارائه ۲
 
جلسه اول پایتون برای هکر های قانونی دوره مقدماتی پاییز ۹۲
جلسه اول پایتون برای هکر های قانونی دوره مقدماتی پاییز ۹۲جلسه اول پایتون برای هکر های قانونی دوره مقدماتی پاییز ۹۲
جلسه اول پایتون برای هکر های قانونی دوره مقدماتی پاییز ۹۲
 
اسلاید اول جلسه اول دوره پاییز کلاس پایتون برای هکرهای قانونی
اسلاید اول جلسه اول دوره پاییز کلاس پایتون برای هکرهای قانونیاسلاید اول جلسه اول دوره پاییز کلاس پایتون برای هکرهای قانونی
اسلاید اول جلسه اول دوره پاییز کلاس پایتون برای هکرهای قانونی
 
اسلاید دوم جلسه یازدهم کلاس پایتون برای هکر های قانونی
اسلاید دوم جلسه یازدهم کلاس پایتون برای هکر های قانونیاسلاید دوم جلسه یازدهم کلاس پایتون برای هکر های قانونی
اسلاید دوم جلسه یازدهم کلاس پایتون برای هکر های قانونی
 
اسلاید اول جلسه یازدهم کلاس پایتون برای هکرهای قانونی
اسلاید اول جلسه یازدهم کلاس پایتون برای هکرهای قانونیاسلاید اول جلسه یازدهم کلاس پایتون برای هکرهای قانونی
اسلاید اول جلسه یازدهم کلاس پایتون برای هکرهای قانونی
 
اسلاید ارائه دوم جلسه ۱۰ کلاس پایتون برای هکر های قانونی
اسلاید ارائه دوم جلسه ۱۰ کلاس پایتون برای هکر های قانونی اسلاید ارائه دوم جلسه ۱۰ کلاس پایتون برای هکر های قانونی
اسلاید ارائه دوم جلسه ۱۰ کلاس پایتون برای هکر های قانونی
 
اسلاید جلسه ۹ کلاس پایتون برای هکر های قانونی
اسلاید جلسه ۹ کلاس پایتون برای هکر های قانونیاسلاید جلسه ۹ کلاس پایتون برای هکر های قانونی
اسلاید جلسه ۹ کلاس پایتون برای هکر های قانونی
 
اسلاید اول جلسه هشتم کلاس پایتون برای هکرهای قانونی
اسلاید اول جلسه هشتم کلاس پایتون برای هکرهای قانونیاسلاید اول جلسه هشتم کلاس پایتون برای هکرهای قانونی
اسلاید اول جلسه هشتم کلاس پایتون برای هکرهای قانونی
 
اسلاید دوم جلسه هفتم کلاس پایتون برای هکرهای قانونی
اسلاید دوم جلسه هفتم کلاس پایتون برای هکرهای قانونیاسلاید دوم جلسه هفتم کلاس پایتون برای هکرهای قانونی
اسلاید دوم جلسه هفتم کلاس پایتون برای هکرهای قانونی
 
اسلاید اول جلسه هفتم کلاس پایتون برای هکرهای قانونی
اسلاید اول جلسه هفتم کلاس پایتون برای هکرهای قانونیاسلاید اول جلسه هفتم کلاس پایتون برای هکرهای قانونی
اسلاید اول جلسه هفتم کلاس پایتون برای هکرهای قانونی
 
اسلاید اول جلسه پنجم کلاس پایتون برای هکرهای قانونی
اسلاید اول جلسه پنجم کلاس پایتون برای هکرهای قانونیاسلاید اول جلسه پنجم کلاس پایتون برای هکرهای قانونی
اسلاید اول جلسه پنجم کلاس پایتون برای هکرهای قانونی
 

Recently uploaded

Activity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfActivity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdf
ciinovamais
 
1029-Danh muc Sach Giao Khoa khoi 6.pdf
1029-Danh muc Sach Giao Khoa khoi  6.pdf1029-Danh muc Sach Giao Khoa khoi  6.pdf
1029-Danh muc Sach Giao Khoa khoi 6.pdf
QucHHunhnh
 
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in DelhiRussian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
kauryashika82
 

Recently uploaded (20)

social pharmacy d-pharm 1st year by Pragati K. Mahajan
social pharmacy d-pharm 1st year by Pragati K. Mahajansocial pharmacy d-pharm 1st year by Pragati K. Mahajan
social pharmacy d-pharm 1st year by Pragati K. Mahajan
 
Sports & Fitness Value Added Course FY..
Sports & Fitness Value Added Course FY..Sports & Fitness Value Added Course FY..
Sports & Fitness Value Added Course FY..
 
Sanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfSanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdf
 
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
 
Advance Mobile Application Development class 07
Advance Mobile Application Development class 07Advance Mobile Application Development class 07
Advance Mobile Application Development class 07
 
Key note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdfKey note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdf
 
The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13
 
Código Creativo y Arte de Software | Unidad 1
Código Creativo y Arte de Software | Unidad 1Código Creativo y Arte de Software | Unidad 1
Código Creativo y Arte de Software | Unidad 1
 
Measures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeMeasures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and Mode
 
Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17
 
Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3
 
Mattingly "AI & Prompt Design: The Basics of Prompt Design"
Mattingly "AI & Prompt Design: The Basics of Prompt Design"Mattingly "AI & Prompt Design: The Basics of Prompt Design"
Mattingly "AI & Prompt Design: The Basics of Prompt Design"
 
Activity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfActivity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdf
 
Measures of Dispersion and Variability: Range, QD, AD and SD
Measures of Dispersion and Variability: Range, QD, AD and SDMeasures of Dispersion and Variability: Range, QD, AD and SD
Measures of Dispersion and Variability: Range, QD, AD and SD
 
1029-Danh muc Sach Giao Khoa khoi 6.pdf
1029-Danh muc Sach Giao Khoa khoi  6.pdf1029-Danh muc Sach Giao Khoa khoi  6.pdf
1029-Danh muc Sach Giao Khoa khoi 6.pdf
 
Disha NEET Physics Guide for classes 11 and 12.pdf
Disha NEET Physics Guide for classes 11 and 12.pdfDisha NEET Physics Guide for classes 11 and 12.pdf
Disha NEET Physics Guide for classes 11 and 12.pdf
 
Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111
 
Accessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactAccessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impact
 
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in DelhiRussian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
 
Introduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsIntroduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The Basics
 

اسلاید ارائه اول جلسه ۱۰ کلاس پایتون برای هکر های قانونی

  • 1. Python for Ethical Hackers Mohammad reza Kamalifard Kamalifard@datasec.ir
  • 2. Python Language Essentials Part 5 : Multi-threaded Programming and Queue Mohammad reza Kamalifard Kamalifard.ir/pysec101
  • 3. Thread Running several threads is similar to running several different programs concurrently, but with the following benefits: Multiple threads within a process share the same data space with the main thread and can therefore share information or communicate with each other more easily than if they were separate processes. Threads sometimes called light-weight processes and they do not require much memory overhead; they care cheaper than processes. Mohammad reza Kamalifard Kamalifard.ir/pysec101
  • 4. Start a newThread Starting a New Thread: To spawn another thread, you need to call following method available in thread module: thread.start_new_thread ( function, args[, kwargs] ) This method call enables a fast and efficient way to create new threads in both Linux and Windows. The method call returns immediately and the child thread starts and calls function with the passed list of agrs. When function returns, the thread terminates. Here, args is a tuple of arguments; use an empty tuple to call function without passing any arguments. kwargs is an optional dictionary of keyword arguments. Mohammad reza Kamalifard Kamalifard.ir/pysec101
  • 5. Start a newThread import thread import time # Define a function for the thread def print_time( threadName, delay): count = 0 while count < 5: time.sleep(delay) count += 1 print "%s: %s" % ( threadName, time.ctime(time.time()) ) # Create two threads as follows thread.start_new_thread( print_time, ("Thread-1", 2, ) ) thread.start_new_thread( print_time, ("Thread-2", 4, ) ) while True: pass Mohammad reza Kamalifard Kamalifard.ir/pysec101
  • 6. The Threading Module The newer threading module included with Python 2.4 provides much more powerful, high-level support for threads than the thread module discussed in the previous section. The threading module exposes all the methods of the thread module and provides some additional methods: threading.activeCount(): Returns the number of thread objects that are active. threading.currentThread(): Returns the number of thread objects in the caller's thread control. threading.enumerate(): Returns a list of all thread objects that are currently active. Mohammad reza Kamalifard Kamalifard.ir/pysec101
  • 7. The Threading Module In addition to the methods, the threading module has the Thread class that implements threading. The methods provided by the Thread class are as follows: run(): The run() method is the entry point for a thread. start(): The start() method starts a thread by calling the run method. join([time]): The join() waits for threads to terminate. isAlive(): The isAlive() method checks whether a thread is still executing. getName(): The getName() method returns the name of a thread. setName(): The setName() method sets the name of a thread. Mohammad reza Kamalifard Kamalifard.ir/pysec101
  • 8. Creating Thread using Threading Module To implement a new thread using the threading module, you have to do the following: Define a new subclass of the Thread class. Override the __init__(self [,args]) method to add additional arguments. Then, override the run(self [,args]) method to implement what the thread should do when started. Once you have created the new Thread subclass, you can create an instance of it and then start a new thread by invoking the start(), which will in turn call run() method. Mohammad reza Kamalifard Kamalifard.ir/pysec101
  • 9. Multi-threaded Queue The Queue module allows you to create a new queue object that can hold a specific number of items. There are following methods to control the Queue: get(): The get() removes and returns an item from the queue. put(): The put adds item to a queue. qsize() : The qsize() returns the number of items that are currently in the queue. empty(): The empty( ) returns True if queue is empty; otherwise, False. full(): the full() returns True if queue is full; otherwise, False. Mohammad reza Kamalifard Kamalifard.ir/pysec101
  • 10. Create task queues Threads receive tasks Threads complete tasks and inform the queue All threads exit once queue is empty finally when all task executed the program exits Mohammad reza Kamalifard Kamalifard.ir/pysec101
  • 11. import threading import Queue import time class WorkerThread(threading.Thread): def __init__(self, queue): threading.Thread.__init__(self) self.queue = queue def run(self): print "In WorkerThread" while True: counter = self.queue.get() print "Ordered to sleep for %d seconds" % counter time.sleep(counter) print "Finished sleeping for %d seconds" % counter self.queue.task_done() Mohammad reza Kamalifard Kamalifard.ir/pysec101
  • 12. queue = Queue.Queue() for i in range(10): print "Creating WorkerThread : % d" % i worker = WorkerThread(queue) worker.setDaemon(True) worker.start() print "WorkerThread %d Created" % i for j in range(10): queue.put(j) queue.join() print "All tasks over!" Mohammad reza Kamalifard Kamalifard.ir/pysec101
  • 13. Creating WorkerThread : 0 WorkerThread 0 Created Creating WorkerThread : 1 In WorkerThread WorkerThread 1 Created Creating WorkerThread : 2 In WorkerThread WorkerThread 2 Created Creating WorkerThread : 3 In WorkerThread WorkerThread 3 Created Creating WorkerThread : 4 In WorkerThread WorkerThread 4 Created Creating WorkerThread : 5 In WorkerThread WorkerThread 5 Created Creating WorkerThread : 6 In WorkerThread Ordered to sleep for 0 seconds Finished sleeping for 0 seconds Ordered to sleep for 1 seconds Ordered to sleep for 2 seconds Ordered to sleep for 3 seconds ... Mohammad reza Kamalifard Kamalifard.ir/pysec101
  • 14. Ordered to sleep for 4 seconds Ordered to sleep for 5 seconds Ordered to sleep for 6 seconds Ordered to sleep for 7 seconds Ordered to sleep for 8 seconds Ordered to sleep for 9 seconds In WorkerThread Finished sleeping for 1 seconds Finished sleeping for 2 seconds Finished sleeping for 3 seconds Finished sleeping for 4 seconds Finished sleeping for 5 seconds Finished sleeping for 6 seconds Finished sleeping for 7 seconds Finished sleeping for 8 seconds Finished sleeping for 9 seconds All tasks over! Mohammad reza Kamalifard Kamalifard.ir/pysec101
  • 15. This work is licensed under the Creative Commons Attribution-NoDerivs 3.0 Unported License. To view a copy of this license, visit http://creativecommons.org/licenses/by-nd/3.0/ Copyright 2013 Mohammad reza Kamalifard. All rights reserved.