Python的循环速度慢,甚至挂起 [英] Python for loop slows and evenutally hangs

查看:449
本文介绍了Python的循环速度慢,甚至挂起的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我完全是Python新手(截至半小时前),并试图编写一个简单的脚本来枚举SMTP服务器上的用户。



用户文件是一个简单的列表(每行一个)用户名。

脚本运行良好,但是循环的每一次迭代都会变慢,直到在循环14周围,似乎完全挂起。没有错误 - 我必须^ c。



TIA,
Tom



>

 #!/ usr / bin / python 

导入套接字
导入sys

if len(sys.argv)!= 2:
print用法:vrfy.py< username file>
sys.exit(0)

#open用户文件
file = open(sys.argv [1],'r')
users = [x。 file.readlines()中的x的strip()]
file.close

#仅用于调试
打印用户

#创建一个套接字
s = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
#连接到服务器
connect = s.connect((''192.168.13.222',25))
$
$ VRFY a用户
s.send('VRFY'+ x +'\r\\\
')
结果= s.recv(1024 )
打印结果

#关闭套接字
s.close()


解决方案

很可能您的SMTP服务器正在暂缓您的客户端连接。这是防范失控客户或提交大量垃圾命令的客户。从postfix smtpd的手册页:

  smtpd_junk_command_limit(正常:100,压力:1)
垃圾命令的数量(NOOP,VRFY,ETRN或RSET)
远程SMTP客户端可以在Postfix SMTP服务器
开始用每个垃圾命令增加错误计数器之前发送。

smtpd守护程序会在看到一定数量的垃圾之前回复1秒的延迟。如果您有权访问有问题的smtp服务器的root权限,请尝试使用strace查看服务器是否正在发送纳休眠系统调用。

这是从我的本地服务器上运行你的脚本的踪迹。在100个VRFY命令之后,它开始在命令之间进行睡眠。您的服务器可能有一个大约15个垃圾命令的下限:

  nanosleep({1,0},0x7fffda9a67a0)= 0 
poll([{fd = 9,events = POLLOUT}],1,300000)= 1([{fd = 9,revents = POLLOUT})
write(9,252 2.0.0 pat \ r \ n,15)= 15
poll([{fd = 9,events = POLLIN}],1,300000)= 1([{fd = 9,revents = POLLIN}])
read(9,VRFY pat \r \ n,4096)= 10


I'm totally new to Python (as of half an hour ago) and trying to write a simple script to enumerate users on an SMTP server.

The users file is a simple list (one per line) of usernames.

The script runs fine but with each iteration of the loop it slows until, around loop 14, it seems to hang completely. No error - I have to ^c.

Can anyone shed some light on the problem please?

TIA, Tom

#!/usr/bin/python

import socket
import sys

if len(sys.argv) != 2:
        print "Usage: vrfy.py <username file>"
        sys.exit(0)

#open user file
file=open(sys.argv[1], 'r')
users=[x.strip() for x in file.readlines()]
file.close

#Just for debugging
print users

# Create a Socket
s=socket.socket(socket.AF_INET, socket.SOCK_STREAM)
# Connect to the Server
connect=s.connect(('192.168.13.222',25))

for x in users:
        # VRFY a user
        s.send('VRFY ' + x + '\r\n')
        result=s.recv(1024)
        print result

# Close the socket
s.close()

解决方案

Most likely your SMTP server is tarpitting your client connection. This is a defense against runaway clients, or clients which submit large volumes of "junk" commands. From the manpage for Postfix smtpd:

   smtpd_junk_command_limit (normal: 100, stress: 1)
          The number of junk commands (NOOP, VRFY, ETRN or  RSET)  that  a
          remote  SMTP  client  can  send  before  the Postfix SMTP server
          starts to increment the error counter with each junk command.

The smtpd daemon will insert a 1-second delay before replying after a certain amount of junk is seen. If you have root access to the smtp server in question, try an strace to see if nanosleep syscalls are being issued by the server.

Here is a trace from running your script against my local server. After 100 VRFY commands it starts sleeping between commands. Your server may have a lower limit of ~15 junk commands:

nanosleep({1, 0}, 0x7fffda9a67a0)       = 0
poll([{fd=9, events=POLLOUT}], 1, 300000) = 1 ([{fd=9, revents=POLLOUT}])
write(9, "252 2.0.0 pat\r\n", 15)       = 15
poll([{fd=9, events=POLLIN}], 1, 300000) = 1 ([{fd=9, revents=POLLIN}])
read(9, "VRFY pat\r\n", 4096)           = 10

这篇关于Python的循环速度慢,甚至挂起的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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