client.py
import socket
def get_ip_status(ip, port):
sk= socket.socket(socket.AF_INET, socket.SOCK_STREAM)
try:
sk.connect((ip, port))
# print('{0} port {1} is open'.format(ip, port))
return True
except Exception as err:
# print('{0} port {1} is not open'.format(ip, port))
return False
finally:
sk.close()
if __name__ == '__main__':
host = '127.0.0.1'
for port in range(9000, 9010):
if get_ip_status(host, port):
print('{0} port {1} is open'.format(host, port))
else:
print('{0} port {1} is not open'.format(host, port))
server.py
import socket
sk=socket.socket()
sk.bind(("127.0.0.1",9005))
sk.listen()
conn,_ = sk.accept() # 开启端口