![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
import sys import re import socket import time import inspect import ctypes import threading from PyQt4 import QtCore,QtGui,uic from PyQt4.QtCore import QObject from PyQt4.QtCore import pyqtSignal from PyQt4.QtCore import pyqtSlot from PyQt4.QtGui import QTableView from PyQt4.QtGui import QStandardItemModel from PyQt4.QtGui import QStandardItem qtCreatorFile="MainForm.ui" Ui_MainWindow,QtBaseClass=uic.loadUiType(qtCreatorFile) portLock=threading.Lock() ipLock=threading.Lock() flagLock=threading.Lock() tableViewLock=threading.Lock() mutex=threading.Lock() now_Port= 0 nowip=0# thread scan now ip row=0 sta_Ip="" end_Ip="" Changeip=False sta_Port=0 end_Port=0 threadNum=0 threads=[] ipr=[]#have three params,staipnum,endidnum,iprange processbar=QtGui.QProgressBar model=QStandardItemModel() class Scanner: def __init__(self): pass def scanPort(self): pass class MyApp(QtGui.QMainWindow,Ui_MainWindow): def __init__(self): QtGui.QMainWindow.__init__(self) Ui_MainWindow.__init__(self) self.setupUi(self) self.pauBtn.setVisible(False) global model,processbar processbar=self.progressBar self.result.setModel(model); self.staBtn.clicked.connect(self.ThreadScan) self.staIp.textChanged.connect(self.setEndIp) self.pauBtn.clicked.connect(self.changeState) self.stopBtn.clicked.connect(self.stopThread) @pyqtSlot(int) def setprocessbar(self,percent): self.progressBar.setValue(percent) @pyqtSlot(str) def nowip(self,ip): self.curip.setText(ip) def changeState(self): if self.pauBtn.text()=="PAUSE": mutex.acquire() self.pauBtn.setText("RESUME") return 0 if self.pauBtn.text()=="RESUME": mutex.release() self.pauBtn.setText("PAUSE") return 0 def stopThread(self): if len(threads)>0: for thread in threads: thread.stop() try: del threads[:] except : print "unknow error!" try: global end_Port processbar.setValue(end_Port) except: pass self.pauBtn.setVisible(False) def getContext(self): global sta_Ip,end_Ip,sta_Port,end_Port,threadNum,processbar sta_Ip=self.staIp.text() end_Ip=self.endIp.text() sta_Port=self.staPort.value() end_Port=self.endPort.value() threadNum=self.threadNum.value() processbar.setRange(sta_Port,end_Port) processbar.setValue(sta_Port) def setEndIp(self): self.endIp.setText(self.staIp.text()) def checkip(self): if re.match(r"^(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?).){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$", self.staIp.text()) and re.match(r"^(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?).){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$", self.endIp.text()): print "IP vaild" return True else: print "IP invaild" self.staIp.setText("IP invaild,please check it") return False def beforstart(self): global sta_Ip, end_Ip, ipr, row self.ipcheck = self.checkip() if not self.ipcheck: return 0 self.getContext() # get the iprange,save in ipr ipr = iprange(sta_Ip, end_Ip) # print len(ipr),ipr # return row = 0 model.clear() model.setItem(row, 0, QStandardItem("now scan ip:")) model.setItem(row, 1, QStandardItem(sta_Ip)) row = row + 1 self.pauBtn.setVisible(True) if len(threads): del threads[:] global now_Port, sta_Port, threadNum, nowip, Changeip now_Port = sta_Port nowip = ipr[0] self.curip.setText(sta_Ip) def ThreadScan(self): self.beforstart() for i in range(0,threadNum,1): tmp_scan=myThread() tmp_scan.setBar.connect(self.setprocessbar) tmp_scan.setIp.connect(self.nowip) tmp_scan.setDaemon(True) threads.append(tmp_scan) print len(threads) for thread in threads: thread.start() class myThread (threading.Thread,QObject): setBar=pyqtSignal(int) setIp=pyqtSignal(str) def __init__(self): threading.Thread.__init__(self) QObject.__init__(self) self.tmpPort=0 self.stopped=False def run(self): #print "run" global now_Port,end_Port,sta_Port,row,sta_Ip,processbar,nowip,Changeip while(not self.stopped): mutex.acquire() self.tmpPort = now_Port self.setBar.emit(self.tmpPort) now_Port = now_Port + 1 print nowip, "---", now_Port if now_Port > end_Port or nowip>ipr[1]: if nowip>ipr[1]: #tableViewLock.acquire() self.setBar.emit(end_Port) mutex.release() #tableViewLock.release() break else: #ipLock.acquire() nowip=nowip+1 sta_Ip=num2ip(nowip) self.setIp.emit(sta_Ip) #tableViewLock.acquire() model.setItem(row,0,QStandardItem("now scan ip:")) model.setItem(row,1,QStandardItem(sta_Ip)) row=row+1 #time.sleep(0.2) #portLock.acquire() now_Port=sta_Port print sta_Ip,"<--->",now_Port #portLock.release() #tableViewLock.release() #ipLock.release() mutex.release() try: s = socket.socket() s.settimeout(0.1) s.connect((sta_Ip, self.tmpPort)) tableViewLock.acquire() # print self.tmpPort,"open " model.setItem(row, 0, QStandardItem(str(self.tmpPort))) model.setItem(row, 1, QStandardItem("opened")) row += 1 tableViewLock.release() except socket.error, e: tableViewLock.acquire() print self.tmpPort,"was not opened " tableViewLock.release() # pass self.stopped=False def stop(self): self.stopped=True def ip2num(ip):#ip to int num lp = [int(x) for x in ip.split('.')] return lp[0] << 24 | lp[1] << 16 | lp[2] << 8 | lp[3] def num2ip(num):# int num to ip ip = ['', '', '', ''] ip[3] = (num & 0xff) ip[2] = (num & 0xff00) >> 8 ip[1] = (num & 0xff0000) >> 16 ip[0] = (num & 0xff000000) >> 24 return '%s.%s.%s.%s' % (ip[0], ip[1], ip[2], ip[3]) def iprange(ip1,ip2): # print ip1,ip2 # return ip1, ip2, 3 num1 = ip2num(ip1) num2 = ip2num(ip2) tmp = num2 - num1 if tmp < 0: return None else: return num1, num2, tmp if "__main__" == __name__: app=QtGui.QApplication(sys.argv) window=MyApp() window.show() sys.exit(app.exec_())
字面意思,用python实现一个端口扫描器。
画重点了:
1 使用 pycharm+pyqt制作可视化的应用软件
2 使用方法:
用pyqtDesigner做好界面,使用如下代码进行调用。
import sys from PyQt4 import QtGui from PyQt4 import uic qtCreatorFile="MainForm.ui" Ui_MainWindow,QtBaseClass=uic.loadUiType(qtCreatorFile) class MyApp(QtGui.QMainWindow,Ui_MainWindow): def __init__(self): QtGui.QMainWindow.__init__(self) Ui_MainWindow.__init__(self) self.setupUi(self) if "__main__" == __name__: app=QtGui.QApplication(sys.argv) window=MyApp() window.show() sys.exit(app.exec_())
这是套路。暂时我还不知道界面切换等高级的使用方法。后期有机会在更新。
补充一下:8012年了,突然要用pyqt做个界面,翻出这个博客,遇到一个新的问题:pip 安装的pyqt5没有下载qt的工具,也就是找不到designer.exe .解决方法:
安装缺少的PyQt5 tools,可使用以下源: pip install PyQt5-tools -i http://pypi.douban.com/simple --trusted-host=pypi.douban.com 安装完成后,在./Python/Lib/site-packages/pyqt5-tools目录下可找到designer.exe,此后可在PyCharm中找到并添加designer.exe --------------------- 本文来自 sinat_21427221 的CSDN 博客 ,全文地址请点击:https://blog.csdn.net/sinat_21427221/article/details/77448857?utm_source=copy