socket setdefaulttimeout [英] socket setdefaulttimeout

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

问题描述

我正在电子邮件过滤脚本中对常见的垃圾邮件黑名单(例如SpamCop ..和

其他人)进行DNS查找。有时,因为解析查找的DNS服务器

可能会下降,所以确保套接字不会挂在那里等待回复。


最近系统升级到Python 2.4.1(从2.2.2开始)之后,我认为我可以利用套接字中的setdefaulttimeout来获得
模块,

限制套接字查找的时间。


作为测试,我将默认的timout设置得非常低。但它没有b $ b似乎有任何影响。套接字将花费几秒钟来完成连接并完成查找,即使我已将

超时设置为百万分之一秒,我认为确保

超时(下面的示例脚本)。


我做错了什么?我误解了什么吗?是什么我想要做的只是不可能?


感谢您的任何提示。示例代码遵循签名......


-

希拉金
http://www.thinkspot.net/sheila/

#!/ usr / local / bin /python2.4

导入套接字

导入sys

从导入时间到时间,asctime,localtime


socket.setdefaulttimeout(.00001)

debugfile =" socketdebug.txt"

def debug(data):

timestamp = str (asctime(localtime(time())))

尝试:

f = open(debugfile,''a'')

f。写(''\ n ***%s *** \ n''%timestamp)

f.write(''%s \''%数据)#24- 2003年12月-ctm-删除了一个

换行

f.close()

除了IOError:

pass

#如果文件无法打开则不执行任何操作

IPaddy =''220.108.204.114''


如果IPaddy:

IPquads = IPaddy.split(''。'')

IPquads.reverse()

reverseIP =''。''。join(IPquads)


bl_list = {'' bl.spamcop.net'':''IP地址%s被拒绝 - 请参阅:

http://spamcop.net/bl.shtml''%IPaddy,\

''relays.ordb.org'':''IP地址%s被拒绝 - 请参阅:

http://ordb.org/''%IPaddy,\

''list.dsbl.org'':''IP地址%s被拒绝 - 请参阅:

http://dsbl.org''%IPaddy}

timing_done = 0

start_time = time()

主机在bl_list.keys()中:

如果主机在bl_list中.keys():

IPlookup ="%s。%s" %(reverseIP,host)

try:

debug(" IPlookup =%s ="%IPlookup)

resolvesIP = socket。 gethostbyname(IPlookup)

debug(" resolvesIP =%s ="%resolvesIP)

如果resolveIP.startswith(''127。''):

end_time = time()

elapsed_time = end_time - start_time

timing_done = 1

debug(" rDNS经过的时间) on bl_list:%f secs"%

elapsed_time)

debug(" exiting - SPAM!id'由%s&%;%主持人)

print bl_list [host]

sys.exit(0)

除了socket.gaierror:

pass

如果不是timing_done:

end_time = time()

elapsed_time = end_time - start_time

debug("第二次尝试:时间已过对于bl_list上的rDNS:%f secs"%

elapsed_time)

I''m doing DNS lookups on common spam blacklists (such as SpamCop..and
others) in an email filtering script. Sometimes, because the DNS server
that is resolving the looksup can go down, it is important to make sure
that the socket doesn''t just hang there waiting for a response.

After a recent system upgrade to Python 2.4.1 (from 2.2.2) I thought I
could take advantage of the setdefaulttimeout in the socket module, to
limit the amount of time the sockets take for a lookup.

As a test, I set the default timout ridiculously low. But it doesn''t
seem to be having any effect. The sockets will take several seconds to
make the connection and complete the lookups, even though I''ve set the
timeout to millionths of a second, which I thought would ensure a
timeout (sample script below).

Am I doing something wrong? Do I misunderstand something? Is what I
want to do simply not possible?

Thanks for any tips. Example code follows signature...

--
Sheila King
http://www.thinkspot.net/sheila/

#!/usr/local/bin/python2.4
import socket
import sys
from time import time, asctime, localtime

socket.setdefaulttimeout(.00001)
debugfile = "socketdebug.txt"
def debug(data):
timestamp = str(asctime(localtime(time())))
try:
f = open(debugfile, ''a'')
f.write(''\n*** %s ***\n'' % timestamp)
f.write(''%s\n'' % data) # 24-Dec-2003 -ctm- removed one
linefeed
f.close()
except IOError:
pass
# do nothing if the file cannot be opened
IPaddy = ''220.108.204.114''

if IPaddy:
IPquads = IPaddy.split(''.'')
IPquads.reverse()
reverseIP = ''.''.join(IPquads)

bl_list = { ''bl.spamcop.net'' : ''IP Address %s Rejected - see:
http://spamcop.net/bl.shtml'' % IPaddy, \
''relays.ordb.org'' : ''IP Address %s Rejected - see:
http://ordb.org/'' % IPaddy, \
''list.dsbl.org'' : ''IP Address %s Rejected - see:
http://dsbl.org'' % IPaddy}

timing_done = 0
start_time = time()
for host in bl_list.keys():
if host in bl_list.keys():
IPlookup = "%s.%s" % (reverseIP, host)
try:
debug(" IPlookup=%s=" % IPlookup)
resolvesIP = socket.gethostbyname(IPlookup)
debug(" resolvesIP=%s=" % resolvesIP)
if resolvesIP.startswith(''127.''):
end_time = time()
elapsed_time = end_time - start_time
timing_done = 1
debug("Time elapsed for rDNS on bl_list: %f secs" %
elapsed_time)
debug("exiting--SPAM! id''d by %s" % host)
print bl_list[host]
sys.exit(0)
except socket.gaierror:
pass
if not timing_done:
end_time = time()
elapsed_time = end_time - start_time
debug("2nd try:Time elapsed for rDNS on bl_list: %f secs" %
elapsed_time)

推荐答案

我注意到setdefaulttimeout正在完成我的

全部内容程序。


我正在测试代码中的一些错误处理,并且我是b / b
引发异常以使代码进入除外 ;块...


发送错误电子邮件通知的部分由于套接字超时而被轰炸。

(好吧,直到我将超时提升到3秒。 ..)


我最后一次在新闻组中询问这个话题......有一段时间

回来(比如2年左右)和某人说因为我试图使用的插座

函数是用C语言编写的,而不是Python编写的那个

我无法使用timeoutsocket模块(这是之前唯一的方法是向Python 2.3设置套接字的超时时间。


我想知道......这是否适用于特定的超时我是' 'm

试图强制执行gethostbyname DNS查询?


见解表示赞赏....

I do note that the setdefaulttimeout is accomplishing something in my
full program.

I am testing some error handling in the code at the moment, and am
raising an exception to make the code go into the "except" blocks...

The part that sends an error email notice bombed due to socket timeout.
(well, until I raised the timeout to 3 seconds...)

The last time I asked about this topic in the newsgroups...was a while
back (like 2 or so years) and someone said that because the socket
function that I''m trying to use is coded in C, rather than Python, that
I could not use the timeoutsocket module (which was the only way prior
to Python 2.3 to set timeouts on sockets).

I wonder...is it possible this applies to the particular timeouts I''m
trying to enforce on the "gethostbyname" DNS lookups?

Insights appreciated....


Sheila King写道:
Sheila King wrote:
我正在进行DNS查找[...]重要的是要确保套接字不会挂在那里等待响应。

最近一次系统升级到Python 2.4.1(从2.2.2开始)我认为我可以利用套接字模块中的setdefaulttimeout来限制套接字进行查找所需的时间。 />
作为测试,我将默认的timout设置得非常低。但它似乎没有任何效果。
I''m doing DNS lookups [...] it is important to make sure
that the socket doesn''t just hang there waiting for a response.

After a recent system upgrade to Python 2.4.1 (from 2.2.2) I thought I
could take advantage of the setdefaulttimeout in the socket module, to
limit the amount of time the sockets take for a lookup.

As a test, I set the default timout ridiculously low. But it doesn''t
seem to be having any effect.




超时适用于该套接字上的网络通信,但是

不要调用socket.gethostbyname之类的调用。 gethostbyname

函数实际上是进入操作系统,它可以看起来在缓存中的名称,或者是一个主机文件,或者查询DNS服务器。 b $ b在自己的套接字上。


现代操作系统通常具有合理的TCP / IP实现,

并且OS将处理合理的应用超时。仍然

gethostbyname及其兄弟可能会对单个

线程事件驱动程序感到痛苦,因为他们可以阻止

重要时间。


在一些较旧的线程系统中,任何系统调用都会在进程中的每个线程中阻止

,并且gethostbyname因为

而臭名昭着举起来。有些系统提供了一个异步的

gethostbyname,但这对Python的库的用户没有帮助。

一些程序员会保留一些额外的进程来/>
处理其主机查找。幸运的是,线程系统现在好了很多,而且应该只阻止线程等待

gethostbyname。

-

--Bryan



The timeout applies to network communication on that socket, but
not to calls such as socket.gethostbyname. The gethostbyname
function actually goes to the operating system, which can look
up the name in a cache, or a hosts file, or query DNS servers
on sockets of its own.

Modern OS''s generally have reasonably TCP/IP implementations,
and the OS will handle applying a reasonable timeout. Still
gethostbyname and its brethren can be a pain for single-
threaded event-driven programs, because they can block for
significant time.

Under some older threading systems, any system call would block
every thread in the process, and gethostbyname was notorious for
holding things up. Some systems offer an asynchronous
gethostbyname, but that doesn''t help users of Python''s library.
Some programmers would keep around a few extra processes to
handle their hosts lookups. Fortunately, threading systems are
now much better, and should only block the thread waiting for
gethostbyname.
--
--Bryan


On 08/12/2005 22:37:22 Bryan Olson< fa ********* @ nowhere.org> ;写道:
On 08/12/2005 22:37:22 Bryan Olson <fa*********@nowhere.org> wrote:
Sheila King写道:
Sheila King wrote:
我正在进行DNS查找[...]重要的是要确保插座不会挂在那里等待响应。
最近系统升级到Python 2.4.1(从2.2.2开始)后我想我可以利用套接字模块中的setdefaulttimeout来限制套接字的时间采取查找。
作为测试,我将默认的timout设置得非常低。但它似乎没有任何效果。
I''m doing DNS lookups [...] it is important to make sure that the socket
doesn''t just hang there waiting for a response. After a recent system upgrade to Python 2.4.1 (from 2.2.2) I thought I
could take advantage of the setdefaulttimeout in the socket module, to
limit the amount of time the sockets take for a lookup. As a test, I set the default timout ridiculously low. But it doesn''t
seem to be having any effect.



超时适用于该套接字上的网络通信,但不适用于
诸如socket.gethostbyname之类的调用。 gethostbyname函数实际上是进入操作系统,它可以在缓存中查找名称,或者在主机文件中查找名称,或者在自己的套接字上查询DNS服务器。
现代操作系统通常具有合理的TCP / IP实现,操作系统将处理合理的超时。仍然gethostbyname和它的兄弟可能是单线程事件驱动程序的痛苦,因为它们可以阻止很长时间。
在一些较旧的线程系统中,任何系统调用都会阻塞进程中的每个
线程,并且gethostbyname因举起内容而臭名昭着。有些系统提供异步gethostbyname,但这并不能帮助Python的库用户。一些程序员会保留一些额外的进程来处理他们的主机查找。幸运的是,线程系统现在好多了,应该只阻止线程等待gethostbyname。


The timeout applies to network communication on that socket, but not to
calls such as socket.gethostbyname. The gethostbyname function actually
goes to the operating system, which can look up the name in a cache, or a
hosts file, or query DNS servers on sockets of its own. Modern OS''s generally have reasonably TCP/IP implementations, and the OS
will handle applying a reasonable timeout. Still gethostbyname and its
brethren can be a pain for single- threaded event-driven programs, because
they can block for significant time. Under some older threading systems, any system call would block every
thread in the process, and gethostbyname was notorious for holding things
up. Some systems offer an asynchronous gethostbyname, but that doesn''t
help users of Python''s library. Some programmers would keep around a few
extra processes to handle their hosts lookups. Fortunately, threading
systems are now much better, and should only block the thread waiting for
gethostbyname.




谢谢,Bryan。我没有做任何线程。但是,当它到达SMTP服务器时,我们在

传入的电子邮件上运行此脚本,并且脚本的最大执行时间为16秒

。通常它们运行的​​时间要少得多。但是,由于黑名单的DNS服务器问题,我们发现了一些事件,其中脚本超过了它的最大运行时间和流程操作系统被

杀死了。这导致电子邮件被放回邮件队列中,以便稍后尝试重新递送

。当然,如果这个问题未被发现,那么

邮件最终可以返回给发件人。如果

gethostbyname阻止并且没有返回,则没有有效的方法从正在运行的过滤器脚本中检查

是否超过时间。 :(


正如我所说,通常这不是问题。但是有一些

事件确实引起了一些问题。几天。我希望

解决它。:/


听起来好像我运气不好。


-

Sheila King
http ://www.thinkspot.net/sheila/



Thanks, Bryan. I''m not doing any threading. But we are running this script on
incoming email as it arrives at the SMTP server, and scripts have a 16 second
max time of execution. Generally they run in much less time. However, we have
seen incidents where, due to issues with the DNS servers for the blacklists,
that the script exceed it''s max time to run and the process was killed by
the OS. This results in the email being placed back into the mail queue for
attempted re-delivery later. Of course, if this issue goes undetected, the
mail can eventually be "returned to sender". There''s no effective way to check
from within the running filter script that the time is not exceeded if the
gethostbyname blocks and doesn''t return. :(

As I said, normally this isn''t a problem. But there have been a handful of
incidents where it did cause issues briefly over a few days. I was hoping to
address it. :/

Sounds like I''m out of luck.

--
Sheila King
http://www.thinkspot.net/sheila/


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

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