Python for loop slows and evenutally hangs

2024/7/27 15:36:20

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/pythonimport socket
import sysif 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 users.send('VRFY ' + x + '\r\n')result=s.recv(1024)print result# Close the socket
s.close()
Answer

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  aremote  SMTP  client  can  send  before  the Postfix SMTP serverstarts 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
https://en.xdnf.cn/q/73129.html

Related Q&A

Jupyter Notebook: Change Data Rate Limit Inside Active Notebook

I have a jupyter notebook where an executed cell gives the following error:IOPub data rate exceeded...I understand this is an option:jupyter notebook --NotebookApp.iopub_data_rate_limit=1.0e10However, …

How to obtain the training error in svm of Scikit-learn?

My question: How do I obtain the training error in the svm module (SVC class)?I am trying to do a plot of error of the train set and test set against the number of training data used ( or other featur…

Flask-Migrate not detecting tables

I have the following project structure:project/__init__.pyfrom flask import Flask from flask_sqlalchemy import SQLAlchemy from flask_migrate import Migratedb = SQLAlchemy() migrate = Migrate()def creat…

Cannot train a neural network solving XOR mapping

I am trying to implement a simple classifier for the XOR problem in Keras. Here is the code:from keras.models import Sequential from keras.layers.core import Dense, Dropout, Activation from keras.optim…

Pyarrow read/write from s3

Is it possible to read and write parquet files from one folder to another folder in s3 without converting into pandas using pyarrow.Here is my code:import pyarrow.parquet as pq import pyarrow as pa imp…

matplotlib draw showing nothing

Im using pythons matplotlib to do some contours using contour and contourf functions. They all work fine when using show, but when I try to use draw() inside a method, I get the matplotlib window but n…

Measure the height of a string in Tkinter Python?

I need the height in pixel of a string in a Tkiner widget. It is the text in a row of a Listbox.I know I can measure the width of a string with tkinter.font.Font.measure. But how can I get the height?…

pandas filter by multiple columns NULL

I have a pandas dataframe like:df = pd.DataFrame({Last_Name: [Smith, None, Brown], First_Name: [John, None, Bill],Age: [35, 45, None]})And could manually filter it using:df[df.Last_Name.isnull() & …

Closed IPython Notebook that was running code

How it works? I got some code running in an IPython Notebook. Some iterative work. Accidentally I closed the browser with the running Notebook, but going back to the IPython Dashboard I see that this …

os.popen().read() - charmap decoding error

I have already read UnicodeDecodeError: charmap codec cant decode byte X in position Y: character maps to <undefined>. While the error message is similar, the code is completely different, becau…