標籤:python socket 連接埠 掃描
前幾天看了個講使用Python掃描連接埠的教程,看了之後自己也寫了個掃描連接埠的指令碼。記錄下來,方便自己以後回顧。
運行效果如下:
650) this.width=650;" src="http://s3.51cto.com/wyfs02/M01/6D/FB/wKioL1VwUlmSOQlFAAF2wYbk_78905.jpg" title="掃描連接埠" alt="wKioL1VwUlmSOQlFAAF2wYbk_78905.jpg" />
具體代碼如下,請指教。
# -*- coding:utf8 -*-#!/usr/bin/python# Python: 2.7.8# Platform: Windows# Authro: wucl# Program: 連接埠掃描# History: 2015.6.1import socket, time, threadsocket.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): 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)
本文出自 “載酒仗劍江湖行” 部落格,請務必保留此出處http://wucl202000.blog.51cto.com/4687508/1658662
使用Python掃描連接埠