-
Notifications
You must be signed in to change notification settings - Fork 0
/
pystman.py
212 lines (162 loc) · 6.33 KB
/
pystman.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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
import asyncio
import sys
import websockets
import base64
from PyQt5.QtCore import QObject, Qt, pyqtSignal, pyqtSlot
from PyQt5.QtWidgets import (
QAction,
QApplication,
QHBoxLayout,
QLineEdit,
QMainWindow,
QMenu,
QPushButton, QSplitter,
QTextEdit,
QToolBar,
QToolButton,
QVBoxLayout,
QWidget,
QSizePolicy,
)
from asyncqt import QEventLoop, asyncSlot
from websockets.exceptions import ConnectionClosed
from QKVLineEdit import KVList
from QTextButtonList import ButtonListView
class MainWindow(QMainWindow):
_SESSION_TIMEOUT = 1.
"""float: Session timeout."""
def __init__(self):
super().__init__()
self.initContent()
self.initToolbar()
self.setGeometry(400, 400, 450, 250)
self.setWindowTitle('Websocket Test Client')
self.show()
def initContent(self):
self.postman = Postman()
self.postman.signal.signal.connect(self.pushToHistory)
self.listyList = ButtonListView(self.postman.setValues)
self.splitter = QSplitter()
self.splitter.addWidget(self.listyList)
self.splitter.addWidget(self.postman)
self.splitter.setStretchFactor(0,1)
self.splitter.setStretchFactor(1,2)
self.setCentralWidget(self.splitter)
def initToolbar(self):
self.toolbar = QToolBar("Main toolbar")
self.addToolBar(self.toolbar)
self.toolbar.addAction(QAction("&New", self))
self.toolbar.setMouseTracking(True)
button_action = QAction("Your Button", self)
button_action.setStatusTip("This is your button")
button_action.triggered.connect(lambda x: print("toolbar button clicked"))
saveButton=QToolButton(self)
saveButton.setPopupMode(QToolButton.InstantPopup)
saveButton.setText("save")
menu = QMenu(saveButton)
menu.addAction(QAction("save all", self))
menu.addAction(QAction("save as", self))
saveButton.setMenu(menu)
self.toolbar.addWidget(saveButton)
self.toolbar.addAction(button_action)
@pyqtSlot(str, dict, str)
def pushToHistory(self, url, params, body):
b = self.listyList.wlist.itemWidget(self.listyList.wlist.item(0))
if b.url != url or b.body != body or b.params != params:
self.listyList.pushToHistory(url, body, params)
def keyPressEvent(self, e):
if e.key() == Qt.Key_Escape:
self.close()
def contextMenuEvent(self, event):
print("context menu")
super(MainWindow, self).contextMenuEvent(event)
class PostmanSignal(QObject):
def __init__(self) -> None:
super(PostmanSignal, self).__init__()
signal = pyqtSignal(str, dict, str)
class Postman(QWidget ):
def __init__(self):
super().__init__()
self.initTopBar()
self.pairList = KVList()
self.vbox.addWidget(self.pairList)
#self.addPairButton = QPushButton("Add Pair", self)
#self.addPairButton.clicked.connect(self.pairList.addKVPair)
#self.removePairButton = QPushButton("Remove Pair", self)
#self.removePairButton.clicked.connect(self.pairList.removeKVPair)
#buttons = QHBoxLayout()
#buttons.addWidget(self.addPairButton)
#buttons.addWidget(self.removePairButton)
#buttonsWidget = QWidget()
#buttonsWidget.setLayout(buttons)
#self.vbox.addWidget(buttonsWidget)
self.body = QTextEdit()
self.vbox.addWidget(self.body)
self.editResponse = QTextEdit()
self.vbox.addWidget(self.editResponse)
self.setLayout(self.vbox)
self.setSizePolicy(QSizePolicy.Expanding, QSizePolicy.Expanding)
def initTopBar(self):
self.vbox = QVBoxLayout()
self.signal = PostmanSignal()
self.urlBar = QLineEdit()
self.topBar = QWidget()
self.topBarLayout = QHBoxLayout()
self.topBarLayout.addWidget(self.urlBar)
self.connectButton = QPushButton("Connect", self)
self.connectButton.clicked.connect(self.sendConnectRequest)
self.topBarLayout.addWidget(self.connectButton)
self.disconnectButton = QPushButton("Disconnect", self)
self.disconnectButton.clicked.connect(self.disconnect)
self.topBarLayout.addWidget(self.disconnectButton)
self.sendButton = QPushButton("Send Text")
self.sendButton.clicked.connect(self.sendMessage)
self.topBarLayout.addWidget(self.sendButton)
self.topBar.setLayout(self.topBarLayout)
self.vbox.addWidget(self.topBar)
def setValues(self, url, params=None, body=None):
self.urlBar.setText(url)
self.pairList.setPairs(params if params else {})
self.body.setText(body if body else '')
@asyncSlot()
async def disconnect(self) -> None:
await self.ws.close()
@asyncSlot()
async def sendConnectRequest(self):
self.signal.signal.emit(
self.urlBar.text(),
self.pairList.getPairs(),
''
)
try:
username = self.pairList.pairs[0].getKey()
password = self.pairList.pairs[0].getValue()
auth_header = 'Basic ' + base64.b64encode(bytes(username + ':' + password, 'utf-8')).decode('utf-8')
self.ws = await websockets.connect(self.urlBar.text(),
extra_headers={'Authorization': auth_header})
except Exception as exc:
print(exc)
self.body.setText(str(exc))
return
else:
print("Connected")
try:
async for recieved in self.ws:
if isinstance(recieved, bytes):
recieved = recieved.decode('utf-8')
self.body.setText(recieved)
except ConnectionClosed as c:
print("Connection Closed: code {} \n {}".format(c.code, c.reason))
@asyncSlot()
async def sendMessage(self):
await self.ws.send(self.editResponse.toPlainText())
app = QApplication(sys.argv)
loop = QEventLoop(app)
# this allows me to use async/await with pyqt.
# Apparently it's not super performant, but I don't really need it to be.
asyncio.set_event_loop(loop)
window = MainWindow()
window.show()
# Start the event loop.
with loop:
sys.exit(loop.run_forever())