python掃描連接埠指令碼

來源:互聯網
上載者:User

標籤:

#!/usr/bin/python
#coding=utf-8
‘‘‘
一、socket模組的類方法:
socket.socket(family,type)——建立並返回一個新的socket對象
socket.getfqdn(name)——將使用點好分割的IP地址字串轉換成一個完整的網域名稱
socket.gethostbyname(name)——將主機名稱解析為一個使用點號分隔的IP地址字串
socket.fromfd(fd,family,type)——從現有檔案描述符建立一個socket對象
二、socket模組的執行個體方法:
sock.bind((adrs,port))——將socket綁定到一個地址和連接埠上
sock.accept()——返回一個客戶機socket(帶有客戶機端的地址資訊)
sock.listen(backlog)——將socket設定成監聽模式,可以監聽backlog外來的連結請求
sock.connect((adrs,port))——將socket串連到定義的主機和連接埠上
sock.recv(buflen[,flags])——從socket中接受資料,最多buflen個字元
sock.send(data[,flags])——通過socket發送資料
sock.close()——關閉socket
sock.getsockopt(lvl,optname)——獲得指定socket選項的值
sock.setsockopt(lvl,optname)——設定製定socket選項的值
‘‘‘
import socket, time, thread
socket.setdefaulttimeout(3)

def socket_port(ip,port):
"""
輸入IP和連接埠號碼,掃描判斷連接埠是否開放
"""
try:
if port>=65535:
print u‘連接埠掃描結束‘
s=socket.socket(socket.AF_INET, socket.SOCK_STREAM)
result=s.connect_ex((ip,port))
if result==0:
lock.acquire()
print ip,u‘:‘,port,u‘連接埠開放‘
lock.release()
s.close()
except:
print u‘連接埠掃描異常‘
def ip_scan(ip):
"""
輸入IP,掃描IP的0-65534連接埠情況
"""
try:
print u‘開始掃描 %s‘ % ip
start_time=time.time()
#for i in range(0,65534):
for i in range(0,3308):
thread.start_new_thread(socket_port,(ip,int(i)))
print u‘掃描連接埠完成,總共用時 :%.2f‘ %(time.time()-start_time)
raw_input("Press Enter to Exit")
except:
print u‘掃描ip出錯‘


if __name__==‘__main__‘:
url=raw_input(‘Input the ip you want to scan:\n‘)
lock=thread.allocate_lock()
ip_scan(url)

python掃描連接埠指令碼

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.