[英]Python2.7 socket error 37 "Operation already in progress"
I'm making a script that reads a file full of proxy servers and checks if they are up or down.我正在制作一个脚本来读取一个充满代理服务器的文件并检查它们是启动还是关闭。
import socket
proxyFile = open("proxies.txt","r");
lines = proxyFile.readlines();
class Colors:
none = "\033[0m";
red = "\033[31m";
green = "\033[32m";
yellow = "\033[33m";
blue = "\033[34m";
purple = "\033[35m";
cyan = "\033[36m";
sock = socket.socket(socket.AF_INET,socket.SOCK_STREAM);
sock.settimeout(3);
for line in lines:
line = line.replace(":"," : ");
reader = line.split();
ip = reader[reader.index(":") - 1];
port = int(reader[reader.index(":") + 1]);
try:
sock.connect((ip,port));
print Colors.cyan + ip + ":" + str(port) + Colors.none + " is " + Colors.green + "UP";
sock.close();
except socket.timeout:
print Colors.cyan + ip + Colors.yellow + ":" + Colors.cyan + str(port) + Colors.none + " is " + Colors.red + "DOWN";
It seems that the file reads fine and the socket creates, but it only connects to one server then it gives the error.似乎文件读取正常并且套接字创建,但它只连接到一台服务器然后它给出错误。
Proxy File:代理文件:
1.0.134.56:8080
1.165.192.248:3128
1.172.185.143:53281
1.179.156.233:8080
1.179.164.213:8080
1.179.185.253:8080
1.192.242.191:3128
1.20.169.166:8080
1.20.179.68:8080
1.20.99.163:8080
You can't re- connect
a socket.您无法重新connect
套接字。 Once it's connected, it's connected.一旦连接,它就被连接了。 Even if you call close
:即使你打电话close
:
all future operations on the socket object will fail.套接字对象上的所有未来操作都将失败。
The right answer is to create a new socket each time through the loop, whether with create_connection
or with socket
and connect
.正确的答案是每次通过循环创建一个新的套接字,无论是使用create_connection
还是使用socket
和connect
。 For example, change your try
block to this:例如,将您的try
块更改为:
try:
sock = socket.socket(socket.AF_INET,socket.SOCK_STREAM);
sock.settimeout(3);
sock.connect((ip,port));
print Colors.cyan + ip + ":" + str(port) + Colors.none + " is " + Colors.green + "UP";
sock.close();
except socket.timeout:
print Colors.cyan + ip + Colors.yellow + ":" + Colors.cyan + str(port) + Colors.none + " is " + Colors.red + "DOWN";
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.