FTP 测试通过
This commit is contained in:
@@ -55,6 +55,12 @@ def parse_pasv(response):
|
||||
|
||||
def test_ftp(host, port):
|
||||
print(f"=== FTP Test: {host}:{port} ===\n")
|
||||
checks = [] # (label, ok, detail)
|
||||
|
||||
def check(label, ok, detail=""):
|
||||
checks.append((label, ok, detail))
|
||||
mark = "PASS" if ok else "FAIL"
|
||||
print(f" [{mark}] {label}{(' - ' + detail) if detail else ''}")
|
||||
|
||||
# 1. Connect
|
||||
print("[1] Connecting to control channel...")
|
||||
@@ -63,8 +69,10 @@ def test_ftp(host, port):
|
||||
try:
|
||||
sock.connect((host, port))
|
||||
print(f" Connected to {host}:{port}")
|
||||
check("Connect", True)
|
||||
except Exception as e:
|
||||
print(f" CONNECT FAILED: {e}")
|
||||
check("Connect", False, str(e))
|
||||
return False
|
||||
|
||||
# 2. Read welcome
|
||||
@@ -72,8 +80,11 @@ def test_ftp(host, port):
|
||||
resp = recv_line(sock)
|
||||
if resp:
|
||||
print(f" {resp.decode(errors='replace').rstrip()}")
|
||||
check("Welcome 220", resp.startswith(b"220"),
|
||||
resp.decode(errors='replace').rstrip())
|
||||
else:
|
||||
print(" NO WELCOME BANNER")
|
||||
check("Welcome 220", False, "no banner")
|
||||
sock.close()
|
||||
return False
|
||||
|
||||
@@ -82,27 +93,35 @@ def test_ftp(host, port):
|
||||
resp = send_cmd(sock, "USER anonymous")
|
||||
if not resp:
|
||||
sock.close()
|
||||
check("Login USER", False, "no response")
|
||||
return False
|
||||
|
||||
resp = send_cmd(sock, "PASS test@test.com")
|
||||
if not resp:
|
||||
sock.close()
|
||||
check("Login PASS", False, "no response")
|
||||
return False
|
||||
login_ok = b"230" in resp
|
||||
check("Login (USER/PASS 230)", login_ok,
|
||||
resp.decode(errors='replace').rstrip())
|
||||
|
||||
# 4. PWD
|
||||
print("\n[4] PWD...")
|
||||
resp = send_cmd(sock, "PWD")
|
||||
pwd_ok = bool(resp) and resp.startswith(b"257")
|
||||
check("PWD 257", pwd_ok,
|
||||
resp.decode(errors='replace').rstrip() if resp else "no response")
|
||||
|
||||
# 5. PASV mode
|
||||
print("\n[5] PASV...")
|
||||
resp = send_cmd(sock, "PASV")
|
||||
if not resp:
|
||||
sock.close()
|
||||
return False
|
||||
|
||||
pasv_ip, pasv_port = parse_pasv(resp)
|
||||
pasv_ip, pasv_port = (None, None)
|
||||
if resp:
|
||||
pasv_ip, pasv_port = parse_pasv(resp)
|
||||
if pasv_port is None and resp and b"227" in resp:
|
||||
# 227 响应但解析端口失败
|
||||
pasv_port = None
|
||||
if pasv_port is None:
|
||||
print(" FAILED to parse PASV response")
|
||||
# Try EPSV
|
||||
print("\n[5b] Trying EPSV...")
|
||||
resp = send_cmd(sock, "EPSV")
|
||||
@@ -113,21 +132,21 @@ def test_ftp(host, port):
|
||||
pasv_port = int(m.group(1))
|
||||
pasv_ip = host
|
||||
print(f" EPSV: {pasv_ip}:{pasv_port}")
|
||||
pasv_ok = pasv_port is not None
|
||||
check("PASV/EPSV data port", pasv_ok,
|
||||
f"{pasv_ip}:{pasv_port}" if pasv_ok else "no port")
|
||||
|
||||
if pasv_port is None:
|
||||
print(" PASV/EPSV FAILED, trying PORT mode...")
|
||||
# Skip data connection tests
|
||||
else:
|
||||
# 6. LIST (correct PASV flow: connect data first, then send LIST)
|
||||
if pasv_ok:
|
||||
print(f" Data channel: {pasv_ip}:{pasv_port}")
|
||||
|
||||
# 6. LIST (correct PASV flow: connect data first, then send LIST)
|
||||
print("\n[6] LIST...")
|
||||
data_sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
|
||||
data_sock.settimeout(5.0)
|
||||
try:
|
||||
data_sock.connect((pasv_ip, pasv_port))
|
||||
print(f" Data connected to {pasv_ip}:{pasv_port}")
|
||||
send_cmd(sock, "LIST")
|
||||
resp = send_cmd(sock, "LIST")
|
||||
list_open = bool(resp) and resp.startswith(b"150")
|
||||
data = b""
|
||||
while True:
|
||||
try:
|
||||
@@ -140,22 +159,41 @@ def test_ftp(host, port):
|
||||
print(f" LIST result ({len(data)} bytes):")
|
||||
print(data.decode(errors='replace'))
|
||||
data_sock.close()
|
||||
resp = recv_line(sock)
|
||||
list_close = bool(resp) and resp.startswith(b"226")
|
||||
print(f" LIST response: "
|
||||
f"{resp.decode(errors='replace').rstrip() if resp else 'none'}")
|
||||
list_ok = list_open and list_close
|
||||
check("LIST 150->226", list_ok,
|
||||
f"open={list_open} close={list_close} bytes={len(data)}")
|
||||
except Exception as e:
|
||||
print(f" LIST FAILED: {e}")
|
||||
data_sock.close()
|
||||
|
||||
# Read LIST response
|
||||
resp = recv_line(sock)
|
||||
if resp:
|
||||
print(f" LIST response: {resp.decode(errors='replace').rstrip()}")
|
||||
check("LIST 150->226", False, str(e))
|
||||
else:
|
||||
print(" Skipping LIST (no data port)")
|
||||
|
||||
# 7. QUIT
|
||||
print("\n[7] QUIT...")
|
||||
send_cmd(sock, "QUIT")
|
||||
|
||||
resp = send_cmd(sock, "QUIT")
|
||||
quit_ok = bool(resp) and resp.startswith(b"221")
|
||||
check("QUIT 221", quit_ok,
|
||||
resp.decode(errors='replace').rstrip() if resp else "no response")
|
||||
sock.close()
|
||||
print("\n=== Test Complete ===")
|
||||
return True
|
||||
|
||||
# Summary
|
||||
passed = all(ok for _, ok, _ in checks)
|
||||
print("\n" + "=" * 40)
|
||||
print("FTP Test Summary:")
|
||||
for label, ok, _ in checks:
|
||||
print(f" [{'PASS' if ok else 'FAIL'}] {label}")
|
||||
print("=" * 40)
|
||||
if passed:
|
||||
print(">>> TEST PASSED <<<")
|
||||
else:
|
||||
print(">>> TEST FAILED <<<")
|
||||
print("=== Test Complete ===")
|
||||
return passed
|
||||
|
||||
def main():
|
||||
parser = argparse.ArgumentParser(description="FTP Server Test")
|
||||
|
||||
Reference in New Issue
Block a user