线程/套接字快速问题。 [英] threads/sockets quick question.

查看:61
本文介绍了线程/套接字快速问题。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这个脚本应该创建单独的线程来扫描一系列IP

地址,但它没有,它很简单......什么都不做。它没有挂起

什么,线程没有被执行,任何想法都没有?

----------

导入套接字

导入线程

导入回溯

MAX_THREADS = 50

类scanThread(线程化) .Thread):

def run(self):

试试:

ss = socket.socket(socket.AF_INET,socket.SOCK_STREAM)

ss.connect((ip,port_counter))

print"%s | %d OPEN %(ip,port_counter)

ss.close()

打印" scanning:",port_counter," \ n"

除外:

traceback.print_exc()

#end class -------------------

def scan(ip,begin,end):

port_counter = 0

for port_counter in range(begin,end):

同时线程< MAX_THREADS:

scanThread()。start()

#end function -------------------

scan(" localhost",0,10000)

this script should create individual threads to scan a range of IP
addresses, but it doesnt, it simple ... does nothing. it doesnt hang
over anything, the thread is not being executed, any ideas anyone?
----------

import socket
import threading
import traceback
MAX_THREADS = 50
class scanThread(threading.Thread):
def run(self):
try:
ss = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
ss.connect((ip, port_counter))
print "%s | %d OPEN" % (ip, port_counter)
ss.close()
print "scanned: ",port_counter,"\n"
except:
traceback.print_exc()
# end class -------------------
def scan(ip, begin, end):
port_counter = 0
for port_counter in range(begin, end):
while threading < MAX_THREADS:
scanThread().start()
# end function -------------------
scan("localhost", 0, 10000)

推荐答案

ed写道:
这个脚本应该创建单独的线程来扫描一系列IP地址,但它不是,它很简单......什么都不做。它没有挂起任何东西,线程没有被执行,任何人的想法?


这是因为错误。什么都没发生,因为


threading< MAX_THREADS


总是假的。您正在将模块与int进行比较。接下来,

你永远不会创建任何scanThread实例。接下来扫描()中的本地

变量在scanThread.run()中不可见。

-

--Bryan


导入套接字
导入线程
导入回溯

MAX_THREADS = 50

类scanThread(threading.Thread):
def run(self):
尝试:
ss = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
ss.connect((ip,port_counter))
print"%s | %d OPEN %(ip,port_counter)
ss.close()
打印"扫描:",port_counter," \ n"
除了:
traceback.print_exc()
#end class -------------------

def scan(ip,begin,end):
port_counter = 0范围内的port_counter(开始,结束):
同时线程< MAX_THREADS:
scanThread()。start()
#end function -------------------
scan(" localhost", 0,10000)
this script should create individual threads to scan a range of IP
addresses, but it doesnt, it simple ... does nothing. it doesnt hang
over anything, the thread is not being executed, any ideas anyone?
It''s because of the bugs. Nothing happens because

threading < MAX_THREADS

is always false. You are comparing a module to an int. Next,
you never create any instances of scanThread. Next the local
variables of scan() are not visible in scanThread.run().
--
--Bryan

import socket
import threading
import traceback
MAX_THREADS = 50
class scanThread(threading.Thread):
def run(self):
try:
ss = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
ss.connect((ip, port_counter))
print "%s | %d OPEN" % (ip, port_counter)
ss.close()
print "scanned: ",port_counter,"\n"
except:
traceback.print_exc()
# end class -------------------
def scan(ip, begin, end):
port_counter = 0
for port_counter in range(begin, end):
while threading < MAX_THREADS:
scanThread().start()
# end function -------------------
scan("localhost", 0, 10000)



导入套接字

导入线程

导入回溯

class scanThread(threading.Thread):

def run(self):

try:

ss = socket.socket(socket) .AF_INET,socket.SOCK_STREAM)

ss.connect((ip,port_counter))

print"%s | %d OPEN %(ip,port_counter)

ss.close()

打印" scanning:",port_counter," \ n"

除外:

traceback.print_exc()

#end class -------------------

def scan(ip,thebegin,theend):

global ip

global thebegin

global theend

port_counter = 0

for port_counter in range(thebegin,theend):

scanThread()。start()

#end function - ------------------

scan(" localhost",0,10000)

import socket
import threading
import traceback
class scanThread(threading.Thread):
def run(self):
try:
ss = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
ss.connect((ip, port_counter))
print "%s | %d OPEN" % (ip, port_counter)
ss.close()
print "scanned: ",port_counter,"\n"
except:
traceback.print_exc()
# end class -------------------
def scan(ip, thebegin, theend):
global ip
global thebegin
global theend
port_counter = 0
for port_counter in range(thebegin, theend):
scanThread().start()
# end function -------------------
scan("localhost", 0, 10000)


Bryan Olson写道:
Bryan Olson wrote:
接下来,你永远不会创建任何scanThread实例。
Next, you never create any instances of scanThread.




认为scanThread()


scanThread()。start()


就是这样做的。


< / F>



one would think that the "scanThread()" part of

scanThread().start()

would do exactly that.

</F>


这篇关于线程/套接字快速问题。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆