Created
April 27, 2014 14:14
-
-
Save e-chernigovskii/11346597 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
__author__ = 'Evgeny Chernigovsky' | |
import sys | |
import queue | |
import threading | |
import subprocess | |
from PySide import QtGui | |
threadCount = 10 | |
class Pinger(QtGui.QMainWindow): | |
def __init__(self): | |
super(Pinger, self).__init__() | |
self.initUI() | |
def initUI(self): | |
openAction = QtGui.QAction(QtGui.QIcon('./icons/open.png'), '&Open', self) | |
openAction.setShortcut('Ctrl+O') | |
openAction.setStatusTip('Open file') | |
openAction.triggered.connect(self.showFileDialog) | |
checkAction = QtGui.QAction(QtGui.QIcon('./icons/start.png'), '&Check', self) | |
checkAction.setShortcut('Ctrl+S') | |
checkAction.setStatusTip('Start checking') | |
checkAction.triggered.connect(self.check) | |
exitAction = QtGui.QAction(QtGui.QIcon('./icons/exit.png'), '&Exit', self) | |
exitAction.setShortcut('Ctrl+Q') | |
exitAction.setStatusTip('Exit application') | |
exitAction.triggered.connect(self.close) | |
self.input = QtGui.QPlainTextEdit() | |
self.output = QtGui.QPlainTextEdit() | |
hbox = QtGui.QHBoxLayout() | |
hbox.addWidget(self.input) | |
hbox.addWidget(self.output) | |
widget = QtGui.QWidget() | |
widget.setLayout(hbox) | |
self.toolbar = self.addToolBar('Zoom') | |
self.toolbar.addAction(openAction) | |
self.toolbar.addAction(checkAction) | |
self.toolbar.addAction(exitAction) | |
self.setCentralWidget(widget) | |
self.setWindowTitle('Pinger') | |
self.setWindowIcon(QtGui.QIcon('./icons/ping.png')) | |
self.show() | |
def showFileDialog(self): | |
fileName = QtGui.QFileDialog.getOpenFileName(self, 'Open file')[0] | |
if fileName == '': | |
return | |
self.input.setPlainText(open(fileName).read()) | |
def check(self): | |
listOfGood = self.getGoodList(self.input.toPlainText().split('\n')) | |
self.output.setPlainText('\n'.join(listOfGood)) | |
def getGoodList(self, listOfServers): | |
self.inputQue = queue.Queue() | |
for x in listOfServers: | |
self.inputQue.put(x) | |
self.outputQue = queue.Queue() | |
for i in range(threadCount): | |
thread = threading.Thread(target=self.ping) | |
thread.setDaemon(True) | |
thread.start() | |
self.inputQue.join() | |
outList = [] | |
while not self.outputQue.empty(): | |
outList.append(self.outputQue.get()) | |
return outList | |
def ping(self): | |
while True: | |
ip = self.inputQue.get() | |
with open('NUL', 'w') as f: | |
returnValue = subprocess.call('ping {0}'.format(ip), stdout=f, stderr=f) | |
if returnValue == 0: | |
self.outputQue.put(ip) | |
self.inputQue.task_done() | |
def main(): | |
app = QtGui.QApplication(sys.argv) | |
pinger = Pinger() | |
sys.exit(app.exec_()) | |
if __name__ == '__main__': | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment