-
Notifications
You must be signed in to change notification settings - Fork 2
/
py-qt-share.py
124 lines (89 loc) · 3.01 KB
/
py-qt-share.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
#!/usr/bin/python3
# -*- coding: utf-8 -*-
import sys
import socketserver
from http.server import *
import threading
import webbrowser
import time
import os
from PyQt5.QtWidgets import *
def handle_server(port, obj, sec):
handler = SimpleHTTPRequestHandler
try:
httpd = socketserver.TCPServer(("", port), handler)
httpd.timeout = 10
url = "http://%s:%s" % (obj.ip, port)
obj.statusLbl.setText("Status: %s %s" % ("Start Server", url))
webbrowser.open(url)
while obj.isActive:
try:
httpd.handle_request()
except:
obj.statusLbl.setText("Status: Ups we're problems")
httpd.server_close()
obj.statusLbl.setText("Status: Off")
except: # We should distinguish here between different kind of errors
for i in range(sec):
if not obj.isActive: break
obj.statusLbl.setText("Status: Port busy... wating %s seconds" % (sec - i) )
time.sleep(1)
if obj.isActive:
handle_server(port, obj, sec + 5)
else:
obj.statusLbl.setText("Status: Off")
class GUI(QWidget):
def __init__(self):
super(GUI, self).__init__()
self.initUI()
def __del__(self):
self.isActive = False
def initUI(self):
grid = QGridLayout()
self.pathLbl = QLabel("Path: ")
self.portLbl = QLabel("HTTP port: ")
self.pathBox = QLineEdit(".")
self.portBox = QSpinBox()
self.browseBtn = QPushButton("...")
self.startBtn = QPushButton("Start")
self.statusLbl = QLabel("Status: Off")
grid.addWidget(self.pathLbl, 0, 0, 1, 1)
grid.addWidget(self.pathBox, 0, 1, 1, 2)
grid.addWidget(self.browseBtn, 0, 3, 1, 1)
grid.addWidget(self.portLbl, 1, 0, 1, 1)
grid.addWidget(self.portBox, 1, 1, 1, 1)
grid.addWidget(self.startBtn, 1, 2, 1, 1)
grid.addWidget(self.statusLbl, 2, 0, 1, 3)
self.setLayout(grid)
self.portBox.setMinimum(1025)
self.portBox.setMaximum(65535)
self.portBox.setValue(8000)
self.port = 8000
self.isActive = False
self.ip = "127.0.0.1"
self.browseBtn.pressed.connect(self.selFolder)
self.startBtn.pressed.connect(self.start)
self.move(300, 150)
self.setWindowTitle('PyShare')
self.show()
def selFolder(self):
self.pathBox.setText(QFileDialog.getExistingDirectory())
def start(self):
if self.isActive == False:
self.port = int(self.portBox.value())
self.isActive = True
path_select = self.pathBox.text()
os.chdir(path_select)
self.startBtn.setText("Stop")
self.threadserver = threading.Thread(target=handle_server, args=(self.port, self, 10, ))
self.threadserver.start()
else:
self.isActive = False
self.startBtn.setText("Start")
self.statusLbl.setText("Status: %s" % ("Stopping Server..."))
def main():
app = QApplication(sys.argv)
ex = GUI()
sys.exit(app.exec_())
if __name__ == '__main__':
main()