socket检测端口是否被占用

import socket
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
result = sock.connect_ex(('127.0.0.1',9222))
if result == 0:
    print("被占用")
else:
    print("没有被暂用")

  目录