---
# System prepended metadata

title: '[PYTHON] 確認主機的port狀態'
tags: [PYTHON, socket]

---

PYTHON 確認主機的port狀態

把ip清單存成txt檔
讀取txt維護較方便


```py=
import socket;

def chechport(ip):

    sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    result = sock.connect_ex((ip,443))
    if result == 0:
        #print(ip,"443 Port is UP.")
        return
    else:
        print(ip,"443 Port is Down!")
    sock.close


f = open('ip.txt', 'r')
lines = f.readlines()
for line in lines:
	line = line.replace('\n','') #去掉換行
	chechport(line)


```