线程帮助 [英] thread help

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

问题描述

你好,


下面是一个脚本,我正在尝试计算公司内部的HTTP

服务器的数量私人网络。有65,536个可能的b $ b主机可能有HTTP服务器。无论如何,我在第一个w / o线程中写了这个脚本

。它可以工作,但它需要几天才能运行,因为它一次探测一个IP ...所以我想我会尝试使它成为线程所以它可以

一次测试几十个IP。


我不是线程专家,远非它。有人能告诉我如何才能使这个工作正常吗?我想同时探测HTTP

服务器的64个唯一IP地址,而不是相同的IP地址64次(因为我现在正在做/
)。任何提示都将不胜感激。


巴特


导入urllib2,套接字,线程,时间


class trivialthread(threading.Thread):

def run(self):

socket.setdefaulttimeout(1)


hosts = []

networks = []


#添加网络192.168.0可能性。

networks.append(" ; 192.168.0。")

n = 0

而n< 255:

n = n + 1

#生成并添加网络192.168.1-255到网络列表。

networks.append(对于网络中的网络,192.168。%s。%(n))




h = 0

#添加nnn0主机可能性

hosts.append(网络+ str(h))

而h< 255:

h = h + 1

#为每个网络添加主机1 - 255.

hosts.append(network + str(h) )


网站=文件(''websites.txt'',''w'')

for ip in hosts:

尝试:

f = urllib2.urlopen(" http://%s"%ip)

f.read()

f.close()

print>>网站,ip

除了urllib2.URLError:

打印ip

除了socket.timeout:

print ip, 超时...................

除了socket.sslerror:

print ip," ; SSL错误..................."

sites.close()


如果__name__ ==''__ main__'':

threads = []

for x in range(64):

thread = trivialthread()

threads.append(线程)

线程中的线程:

thread.start()

同时线程化.activeCount()> 0:

print str(threading.activeCount()),运行包含的线程。 main"

time.sleep(1)

解决方案

在文章< ca ******** **@solaris.cc.vt.edu> ;,

Bart Nessux< ba ********* @ hotmail.com>写道:


我不是线程专家,远非如此。有人能告诉我如何才能使这项工作正常进行吗?我想同时为HTTP
服务器探测64个唯一的IP地址,而不是相同的IP地址64次(就像我现在正在做的那样)。任何提示都将非常感激。




创建一个threading.Thread子类,它接受一个I​​P地址和一个列表

的端口进行扫描。启动此类的64个实例,每个实例都有一个

不同的IP地址。

-

Aahz(aa**@pythoncraft.com) < * GT; http://www.pythoncraft.com/


只要我们喜欢相同的操作系统,事情就很酷。 --piranha


Aahz写道:

Bart Nessux< ba ********* @ hotmail.com>写道:

有人能告诉我如何使这项工作正常吗?我想同时探测HTTP服务器的64个唯一IP地址,...


创建一个threading.Thread子类,它接受一个I​​P地址和一个列表来扫描。启动此类的64个实例,每个实例都有一个不同的IP地址。




另一种方法是创建一个将IP地址推送到其中的que />
联系,并让每个线程在队列中读取地址时,它们可以免费处理它们。这样做的好处是可以将线程数量与您要检查的地址数量分离。


-Scott David Daniels
Sc *********** @ Acm.Org


文章< 40 ******** @ nntp0.pdx.net>,

Scott David Daniels< Sc ******* ****@Acm.Org>写道:

Aahz写道:

Bart Nessux< ba ********* @ hotmail.com>写道:


有人能告诉我如何使这项工作正常吗?我想同时探测HTTP服务器的64个唯一IP地址,...



创建一个带有一个IP地址和一个列表的threading.Thread子类
要扫描的端口启动此类的64个实例,每个实例都有一个不同的IP地址。



另一种方法是创建一个将IP地址推送到其中的que,
contact,并且当每个线程可以自由地处理它们时,让每个线程从队列中读取地址。这样做的好处是可以将线程的数量与要检查的地址数量分离。




当然,这需要更多的工作一个不熟悉线程的人。

-

Aahz(aa**@pythoncraft.com)< * > http://www.pythoncraft.com/


只要我们喜欢相同的操作系统,事情就很酷。 - piranha


Howdy,

Below is a script that I''m using to try and count the number of HTTP
servers within a company''s private network. There are 65,536 possible
hosts that may have HTTP servers on them. Any way, I wrote this script
at first w/o threads. It works, but it takes days to run as it probes
one IP at a time... so I thought I''d try to make it threaded so it could
test several dozen IPs at once.

I''m no expert on threading, far from it. Could someone show me how I can
make this work correctly? I want to probe 64 unique IP address for HTTP
servers simultaneously, not the same IP addy 64 times (as I''m doing
now). Any tips would be much appreciated.

Bart

import urllib2, socket, threading, time

class trivialthread(threading.Thread):
def run(self):
socket.setdefaulttimeout(1)

hosts = []
networks = []

# Add the network 192.168.0 possibility.
networks.append("192.168.0.")
n = 0
while n < 255:
n = n + 1
# Generate and add networks 192.168.1-255 to the list of networks.
networks.append("192.168.%s." %(n))

for network in networks:
h = 0
# Add the n.n.n.0 host possibility
hosts.append(network+str(h))
while h < 255:
h = h + 1
# Add hosts 1 - 255 to each network.
hosts.append(network+str(h))

websites = file(''websites.txt'', ''w'')
for ip in hosts:
try:
f = urllib2.urlopen("http://%s" %ip)
f.read()
f.close()
print>> websites, ip
except urllib2.URLError:
print ip
except socket.timeout:
print ip, "Timed Out..................."
except socket.sslerror:
print ip, "SSL Error..................."
websites.close()

if __name__ == ''__main__'':
threads = []
for x in range(64):
thread = trivialthread()
threads.append(thread)
for thread in threads:
thread.start()
while threading.activeCount() > 0:
print str(threading.activeCount()), "threads running incl. main"
time.sleep(1)

解决方案

In article <ca**********@solaris.cc.vt.edu>,
Bart Nessux <ba*********@hotmail.com> wrote:


I''m no expert on threading, far from it. Could someone show me how I can
make this work correctly? I want to probe 64 unique IP address for HTTP
servers simultaneously, not the same IP addy 64 times (as I''m doing
now). Any tips would be much appreciated.



Create a threading.Thread subclass that takes one IP address and a list
of ports to scan. Start 64 instances of this class, each with a
different IP address.
--
Aahz (aa**@pythoncraft.com) <*> http://www.pythoncraft.com/

"as long as we like the same operating system, things are cool." --piranha


Aahz wrote:

Bart Nessux <ba*********@hotmail.com> wrote:

Could someone show me how I can make this work correctly? I want to probe
64 unique IP address for HTTP servers simultaneously, ...

Create a threading.Thread subclass that takes one IP address and a list
of ports to scan. Start 64 instances of this class, each with a
different IP address.



An alternative is to create a que into which you push IP addresses to
contact, and have each thread read addresses off the queue when they are
free to process them. This has the advantage of decoupling the number
of threads from the number of addresses you want to examine.

-Scott David Daniels
Sc***********@Acm.Org


In article <40********@nntp0.pdx.net>,
Scott David Daniels <Sc***********@Acm.Org> wrote:

Aahz wrote:

Bart Nessux <ba*********@hotmail.com> wrote:


Could someone show me how I can make this work correctly? I want to probe
64 unique IP address for HTTP servers simultaneously, ...



Create a threading.Thread subclass that takes one IP address and a list
of ports to scan. Start 64 instances of this class, each with a
different IP address.



An alternative is to create a que into which you push IP addresses to
contact, and have each thread read addresses off the queue when they are
free to process them. This has the advantage of decoupling the number
of threads from the number of addresses you want to examine.



Absolutely, but that requires a bit more work for someone who isn''t
already familiar with threading.
--
Aahz (aa**@pythoncraft.com) <*> http://www.pythoncraft.com/

"as long as we like the same operating system, things are cool." --piranha


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

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