下面是一个使用Python验证socks5代理速度的示例代码:
python
复制
插入
import socket
import time
def test_socket(proxy):
try:
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.settimeout(5)
start_time = time.time()
s.connect(proxy)
elapsed_time = time.time() - start_time
print("Proxy", proxy[0], ":", proxy[1], "is OK. Elapsed time:", elapsed_time)
return True
except Exception as e:
print("Proxy", proxy[0], ":", proxy[1], "is NOT OK. Error:", str(e))
return False
def test_socks5_proxy(proxy):
# Create socket object
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.settimeout(5)
# Connect to SOCKS5 server
try:
s.connect(proxy)
except Exception as e:
print("Error connecting to SOCKS5 server:", str(e))
return False
# Send SOCKS5 authentication request
s.sendall(b"\x05\x01\x00")
# Receive authentication response
data = s.recv(1024)
if data[0] != 5 or data[1] != 0:
print("Invalid SOCKS5 authentication response:", data)
return False
# Send SOCKS5 connection request
host = socket.gethostbyname('www.google.com')
port = 80
cmd = b"\x05\x01\x00\x01" + socket.inet_aton(host) + port.to_bytes(2, byteorder='big')
s.sendall(cmd)
# Receive connection response
data = s.recv(1024)
if data[0] != 5 or data[1] != 0 or data[2] != 0x00:
print("Invalid SOCKS5 connection response:", data)
return False
# Test socket connection
if not test_socket((proxy[0], proxy[1])):
return False
return True
if name == 'main':
proxies = [
('127.0.0.1', 1080),
('127.0.0.1', 1081),
('127.0.0.1', 1082),
]
for proxy in proxies:
test_socks5_proxy(proxy)
复制
插入
代码中,我们首先定义了一个test_socket函数,用于测试代理能否成功连接到指定的代理服务器。然后,定义了一个test_socks5_proxy函数,用于测试代理的SOCKS5连接,并在连接成功后调用test_socket函数测试代理的连接速度。
最后,我们使用一个包含多个代理地址的列表proxies,对代码进行测试。