In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-02 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
In this issue, the editor will bring you about how to write a port scanner with python and various concurrent attempts. The article is rich in content and analyzes and narrates for you from a professional point of view. I hope you can get something after reading this article.
The principle of the port scanner is very simple, nothing more than operating socket, can connect that this port is open.
Import socketdef scan (port): s = socket.socket () if s.connect_ex (('localhost', port)) = = 0: print port,' open' s.close () if _ _ name__ = ='_ main__': map (scan,range (1Magne65536))
Such a simplest port scanner has come out.
Wait, wait.
We added the timeout to it ourselves.
S.settimeout (0.1)
Run again, it feels much faster.
Multithreaded version import socketimport threadingdef scan (port): s = socket.socket () s.settimeout (0.1) if s.connect_ex (('localhost', port)) = = 0: print port,' open' s.close () if _ _ name__ = ='_ _ main__': threads = [threading.Thread (target=scan, args= (I,)) for i in xrange (1Magazine 65536)] map (lambda x:x.start (), threads)
Run it. Wow, that's fast. It's time to throw a mistake. Thread.error: can't start new thread .
Come to think of it, this process has 65535 threads open, and there are two possibilities: one is that the maximum number of threads has been exceeded, and the other is that the maximum number of socket handles has been exceeded. In linux, you can modify it through ulimit.
If you don't change the maximum limit, how can you use multiple threads not to report an error?
Add a queue, become the producer-consumer mode, open a fixed thread.
Multithreading + queue version import socketimport threadingfrom Queue import Queuedef scan (port): s = socket.socket () s.settimeout (0.1) if s.connect_ex (('localhost', port)) = 0: print port 'open' s.close () def worker (): while not q.empty (): port = q.get () try: scan (port) finally: q.task_done () if _ _ name__ =' _ _ main__': Q = Queue () map (q.put Xrange (1mam 65535) threads = [threading.Thread (target=worker) for i in xrange (500)] map (lambda x:x.start (), threads) q.join ()
There are 500 threads here, constantly fetching tasks from the queue to do.
Multiprocessing+ queue version
You can't start 65535 processes, can you? Or use the producer-consumer model
Import multiprocessingdef scan (port): s = socket.socket () s.settimeout (0.1) if s.connect_ex (('localhost', port)) = 0: print port 'open' s.close () def worker (Q): while not q.empty (): port = q.get () try: scan (port) finally: q.task_done () if _ _ name__ = =' _ _ main__': Q = multiprocessing.JoinableQueue () map (q.putMaery xrange (1m 65535)) jobs = [multiprocessing.Process (target=worker, args= (Q)) ) for i in xrange] map (lambda x:x.start (), jobs)
Notice here that the queue is passed into worker as a parameter, because it is the queue of process safe, otherwise an error will be reported.
Also useful is JoinableQueue (), which, as the name implies, can be join ().
Spawn version of gevent from geventimport monkey; monkey.patch_all (); import geventimport socket...if _ _ name__ = ='_ _ main__': threads = [gevent.spawn (scan, I) for i in xrange (1m 65536)] gevent.joinall (threads)
Note that monkey patch must import before what is patch, otherwise it will Exception KeyError. For example, you can't import threading first, then monkey patch.
Pool version of gevent from gevent import monkey; monkey.patch_all () Import socketfrom gevent.pool import Pool...if _ _ name__ = ='_ _ main__': pool = Pool (500) pool.map (scan,xrange (1Magne65536)) pool.join () concurrent.futures version import socketfrom Queue import Queuefrom concurrent.futures import ThreadPoolExecutor...if _ _ name__ = ='_ main__': Q = Queue () map (q.put Xrange (with ThreadPoolExecutor 65536) with ThreadPoolExecutor (max_workers=500) as executor: for i in range (500): executor.submit (worker,q) the above is the editor's share of how to write a port scanner with python and various concurrent attempts If you happen to have similar doubts, you might as well refer to the above analysis to understand. If you want to know more about it, you are welcome to follow the industry information channel.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.