- ubuntu12.04环境下使用kvm ioctl接口实现最简单的虚拟机
- Ubuntu 通过无线网络安装Ubuntu Server启动系统后连接无线网络的方法
- 在Ubuntu上搭建网桥的方法
- ubuntu 虚拟机上网方式及相关配置详解
CFSDN坚持开源创造价值,我们致力于搭建一个资源共享平台,让每一个IT人在这里找到属于你的精彩世界.
这篇CFSDN的博客文章python3+PyQt5 创建多线程网络应用-TCP客户端和TCP服务器实例由作者收集整理,如果你对这篇文章有兴趣,记得点赞哟.
本文在上文的基础上重新实现支持多线程的服务器.
以下为tcp客户端的程序代码:
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
|
#!/usr/bin/env python3
import
sys
from
pyqt5.qtcore
import
(qbytearray, qdatastream, qdate, qiodevice,
qregexp, qt)
from
pyqt5.qtwidgets
import
(qapplication, qdateedit, qframe, qgridlayout,
qhboxlayout, qlabel, qlineedit, qpushbutton,
qwidget)
from
pyqt5.qtgui
import
qregexpvalidator
from
pyqt5.qtnetwork
import
(qtcpsocket,)
mac
=
true
try
:
from
pyqt5.qtgui
import
qt_mac_set_native_menubar
except
importerror:
mac
=
false
port
=
9407
sizeof_uint16
=
2
class
buildingservicesclient(qwidget):
def
__init__(
self
, parent
=
none):
super
(buildingservicesclient,
self
).__init__(parent)
self
.socket
=
qtcpsocket()
self
.nextblocksize
=
0
self
.request
=
none
roomlabel
=
qlabel(
"&room"
)
self
.roomedit
=
qlineedit()
roomlabel.setbuddy(
self
.roomedit)
regex
=
qregexp(r
"[0-9](?:0[1-9]|[12][0-9]|3[0-4])"
)
self
.roomedit.setvalidator(qregexpvalidator(regex,
self
))
self
.roomedit.setalignment(qt.alignright|qt.alignvcenter)
datelabel
=
qlabel(
"&date"
)
self
.dateedit
=
qdateedit()
datelabel.setbuddy(
self
.dateedit)
self
.dateedit.setalignment(qt.alignright|qt.alignvcenter)
self
.dateedit.setdate(qdate.currentdate().adddays(
1
))
self
.dateedit.setdisplayformat(
"yyyy-mm-dd"
)
responselabel
=
qlabel(
"response"
)
self
.responselabel
=
qlabel()
self
.responselabel.setframestyle(qframe.styledpanel|qframe.sunken)
self
.bookbutton
=
qpushbutton(
"&book"
)
self
.bookbutton.setenabled(false)
self
.unbookbutton
=
qpushbutton(
"&unbook"
)
self
.unbookbutton.setenabled(false)
quitbutton
=
qpushbutton(
"&quit"
)
if
not
mac:
self
.bookbutton.setfocuspolicy(qt.nofocus)
self
.unbookbutton.setfocuspolicy(qt.nofocus)
buttonlayout
=
qhboxlayout()
buttonlayout.addwidget(
self
.bookbutton)
buttonlayout.addwidget(
self
.unbookbutton)
buttonlayout.addstretch()
buttonlayout.addwidget(quitbutton)
layout
=
qgridlayout()
layout.addwidget(roomlabel,
0
,
0
)
layout.addwidget(
self
.roomedit,
0
,
1
)
layout.addwidget(datelabel,
0
,
2
)
layout.addwidget(
self
.dateedit,
0
,
3
)
layout.addwidget(responselabel,
1
,
0
)
layout.addwidget(
self
.responselabel,
1
,
1
,
1
,
3
)
layout.addlayout(buttonlayout,
2
,
1
,
1
,
4
)
self
.setlayout(layout)
self
.socket.connected.connect(
self
.sendrequest)
self
.socket.readyread.connect(
self
.readresponse)
self
.socket.disconnected.connect(
self
.serverhasstopped)
#self.connect(self.socket,
# signal("error(qabstractsocket::socketerror)"),
# self.serverhaserror)
self
.socket.error.connect(
self
.serverhaserror)
self
.roomedit.textedited.connect(
self
.updateui)
self
.dateedit.datechanged.connect(
self
.updateui)
self
.bookbutton.clicked.connect(
self
.book)
self
.unbookbutton.clicked.connect(
self
.unbook)
quitbutton.clicked.connect(
self
.close)
self
.setwindowtitle(
"building services"
)
def
updateui(
self
):
enabled
=
false
if
(
self
.roomedit.text()
and
self
.dateedit.date() > qdate.currentdate()):
enabled
=
true
if
self
.request
is
not
none:
enabled
=
false
self
.bookbutton.setenabled(enabled)
self
.unbookbutton.setenabled(enabled)
def
closeevent(
self
, event):
self
.socket.close()
event.accept()
def
book(
self
):
self
.issuerequest(
"book"
,
self
.roomedit.text(),
self
.dateedit.date())
def
unbook(
self
):
self
.issuerequest(
"unbook"
,
self
.roomedit.text(),
self
.dateedit.date())
def
issuerequest(
self
, action, room, date):
self
.request
=
qbytearray()
stream
=
qdatastream(
self
.request, qiodevice.writeonly)
stream.setversion(qdatastream.qt_5_7)
stream.writeuint16(
0
)
stream.writeqstring(action)
stream.writeqstring(room)
stream << date
stream.device().seek(
0
)
stream.writeuint16(
self
.request.size()
-
sizeof_uint16)
#overwrite seek(0)
self
.updateui()
if
self
.socket.isopen():
self
.socket.close()
self
.responselabel.settext(
"connecting to server..."
)
self
.socket.connecttohost(
"localhost"
, port)
def
sendrequest(
self
):
self
.responselabel.settext(
"sending request..."
)
self
.nextblocksize
=
0
self
.socket.write(
self
.request)
self
.request
=
none
def
readresponse(
self
):
stream
=
qdatastream(
self
.socket)
stream.setversion(qdatastream.qt_5_7)
while
true:
if
self
.nextblocksize
=
=
0
:
if
self
.socket.bytesavailable() < sizeof_uint16:
break
self
.nextblocksize
=
stream.readuint16()
if
self
.socket.bytesavailable() <
self
.nextblocksize:
break
action
=
""
room
=
""
date
=
qdate()
#stream >> action >> room
action
=
stream.readqstring()
room
=
stream.readqstring()
if
action !
=
"error"
:
stream >> date
if
action
=
=
"error"
:
msg
=
"error: {0}"
.
format
(room)
elif
action
=
=
"book"
:
msg
=
"booked room {0} for {1}"
.
format
(room,date.tostring(qt.isodate))
elif
action
=
=
"unbook"
:
msg
=
"unbooked room {0} for {1}"
.
format
(room,date.tostring(qt.isodate))
self
.responselabel.settext(msg)
self
.updateui()
self
.nextblocksize
=
0
def
serverhasstopped(
self
):
self
.responselabel.settext(
"error: connection closed by server"
)
self
.socket.close()
def
serverhaserror(
self
, error):
self
.responselabel.settext(
"error: {0}"
.
format
(
self
.socket.errorstring()))
self
.socket.close()
app
=
qapplication(sys.argv)
form
=
buildingservicesclient()
form.show()
app.exec_()
|
以下为tcp服务端的程序代码:
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
212
213
|
#!/usr/bin/env python3
import
bisect
import
collections
import
sys
from
pyqt5.qtcore
import
(qbytearray, qdatastream, qdate, qreadwritelock, qthread,qiodevice, qt)
from
pyqt5.qtwidgets
import
(qapplication, qmessagebox, qpushbutton)
from
pyqt5.qtnetwork
import
(qabstractsocket,qhostaddress, qtcpserver, qtcpsocket)
port
=
9407
sizeof_uint16
=
2
max_bookings_per_day
=
5
# key = date, value = list of room ids
bookings
=
collections.defaultdict(
list
)
def
printbookings():
for
key
in
sorted
(bookings):
print
(key, bookings[key])
print
()
class
thread(qthread):
lock
=
qreadwritelock()
def
__init__(
self
, socketid, parent):
super
(thread,
self
).__init__(parent)
self
.socketid
=
socketid
def
run(
self
):
socket
=
qtcpsocket()
if
not
socket.setsocketdescriptor(
self
.socketid):
#self.emit(signal("error(int)"), socket.error())
self
.error.connect(socket.error)
return
while
socket.state()
=
=
qabstractsocket.connectedstate:
nextblocksize
=
0
stream
=
qdatastream(socket)
stream.setversion(qdatastream.qt_5_7)
if
(socket.waitforreadyread()
and
socket.bytesavailable() >
=
sizeof_uint16):
nextblocksize
=
stream.readuint16()
else
:
self
.senderror(socket,
"cannot read client request"
)
return
if
socket.bytesavailable() < nextblocksize:
if
(
not
socket.waitforreadyread(
60000
)
or
socket.bytesavailable() < nextblocksize):
self
.senderror(socket,
"cannot read client data"
)
return
action
=
""
room
=
""
date
=
qdate()
action
=
stream.readqstring()
if
action
in
(
"book"
,
"unbook"
):
room
=
stream.readqstring()
stream >> date
try
:
thread.lock.lockforread()
bookings
=
bookings.get(date.topydate())
finally
:
thread.lock.unlock()
uroom
=
str
(room)
if
action
=
=
"book"
:
newlist
=
false
try
:
thread.lock.lockforread()
if
bookings
is
none:
newlist
=
true
finally
:
thread.lock.unlock()
if
newlist:
try
:
thread.lock.lockforwrite()
bookings
=
bookings[date.topydate()]
finally
:
thread.lock.unlock()
error
=
none
insert
=
false
try
:
thread.lock.lockforread()
if
len
(bookings) < max_bookings_per_day:
if
uroom
in
bookings:
error
=
"cannot accept duplicate booking"
else
:
insert
=
true
else
:
error
=
"{0} is fully booked"
.
format
(date.tostring(qt.isodate))
finally
:
thread.lock.unlock()
if
insert:
try
:
thread.lock.lockforwrite()
bisect.insort(bookings, uroom)
finally
:
thread.lock.unlock()
self
.sendreply(socket, action, room, date)
else
:
self
.senderror(socket, error)
elif
action
=
=
"unbook"
:
error
=
none
remove
=
false
try
:
thread.lock.lockforread()
if
bookings
is
none
or
uroom
not
in
bookings:
error
=
"cannot unbook nonexistent booking"
else
:
remove
=
true
finally
:
thread.lock.unlock()
if
remove:
try
:
thread.lock.lockforwrite()
bookings.remove(uroom)
finally
:
thread.lock.unlock()
self
.sendreply(socket, action, room, date)
else
:
self
.senderror(socket, error)
else
:
self
.senderror(socket,
"unrecognized request"
)
socket.waitfordisconnected()
try
:
thread.lock.lockforread()
printbookings()
finally
:
thread.lock.unlock()
def
senderror(
self
, socket, msg):
reply
=
qbytearray()
stream
=
qdatastream(reply, qiodevice.writeonly)
stream.setversion(qdatastream.qt_5_7)
stream.writeuint16(
0
)
stream.writeqstring(
"error"
)
stream.writeqstring(msg)
stream.device().seek(
0
)
stream.writeuint16(reply.size()
-
sizeof_uint16)
socket.write(reply)
def
sendreply(
self
, socket, action, room, date):
reply
=
qbytearray()
stream
=
qdatastream(reply, qiodevice.writeonly)
stream.setversion(qdatastream.qt_5_7)
stream.writeuint16(
0
)
stream.writeqstring(action)
stream.writeqstring(room)
stream<<date
stream.device().seek(
0
)
stream.writeuint16(reply.size()
-
sizeof_uint16)
socket.write(reply)
class
tcpserver(qtcpserver):
def
__init__(
self
, parent
=
none):
super
(tcpserver,
self
).__init__(parent)
def
incomingconnection(
self
, socketid):
thread
=
thread(socketid,
self
)
#self.connect(thread, signal("finished()"),
# thread, slot("deletelater()"))
thread.finished.connect(thread.deletelater)
thread.start()
class
buildingservicesdlg(qpushbutton):
def
__init__(
self
, parent
=
none):
super
(buildingservicesdlg,
self
).__init__(
"&close server"
, parent)
self
.setwindowflags(qt.windowstaysontophint)
self
.loadbookings()
self
.tcpserver
=
tcpserver(
self
)
if
not
self
.tcpserver.listen(qhostaddress(
"0.0.0.0"
), port):
qmessagebox.critical(
self
,
"building services server"
,
"failed to start server: {0}"
.
format
(
self
.tcpserver.errorstring()))
self
.close()
return
self
.clicked.connect(
self
.close)
font
=
self
.font()
font.setpointsize(
24
)
self
.setfont(font)
self
.setwindowtitle(
"building services server"
)
def
loadbookings(
self
):
# generate fake data
import
random
today
=
qdate.currentdate()
for
i
in
range
(
10
):
date
=
today.adddays(random.randint(
7
,
60
))
for
j
in
range
(random.randint(
1
, max_bookings_per_day)):
# rooms are 001..534 excl. 100, 200, ..., 500
floor
=
random.randint(
0
,
5
)
room
=
random.randint(
1
,
34
)
bookings
=
bookings[date.topydate()]
if
len
(bookings) >
=
max_bookings_per_day:
continue
bisect.insort(bookings,
"{0:1d}{1:02d}"
.
format
(
floor, room))
printbookings()
app
=
qapplication(sys.argv)
form
=
buildingservicesdlg()
form.show()
form.move(
0
,
0
)
app.exec_()
|
以上这篇python3+pyqt5 创建多线程网络应用-tcp客户端和tcp服务器实例就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持我.
原文链接:https://blog.csdn.net/xiaoyangyang20/article/details/71375442 。
最后此篇关于python3+PyQt5 创建多线程网络应用-TCP客户端和TCP服务器实例的文章就讲到这里了,如果你想了解更多关于python3+PyQt5 创建多线程网络应用-TCP客户端和TCP服务器实例的内容请搜索CFSDN的文章或继续浏览相关文章,希望大家以后支持我的博客! 。
谁能解释一下 Server.MapPath(".")、Server.MapPath("~")、Server.MapPath(@"之间的区别\") 和 Server.MapPath("/")? 最佳答案
我不知道,为什么我们要使用 Server.UrlEncode() & Server.UrlDecode()?!在 QueryString 中我们看到 URL 中的任何内容,那么为什么我们要对它们进行编
我已经通过 WHM 在我的一个域上安装了 ssl 证书。网站正在使用 https://xyz.com . 但是它不适用于 https://www.xyz.com .我已经检查了证书,它也适用于 www
我已经使用 WMI 检测操作系统上是否存在防病毒软件,itz 正常工作并通过使用命名空间向我显示防病毒信息,例如 win xp 和 window7 上的名称和实例 ID:\root\SecurityC
我们有 hive 0.10 版本,我们想知道是否应该使用 Hive Server 1 或 Hive Server2。另一个问题是连接到在端口 10000 上运行的 Hive 服务器,使用 3rd 方工
我想在 C++ 中使用 Windows Server API 设置一个 HTTPS 服务器,我使用了示例代码,它在 HTTP 上工作正常,但我就是不能让它在 HTTPS 上工作。 (我不想要客户端 S
我写了一个非常基本的类来发送电子邮件。我用 smtp 服务器对其进行了测试,它工作正常,但是当我尝试使用我公司的交换服务器时,它给出了这个异常: SMTP 服务器需要安全连接或客户端未通过身份验证。服
我的应用程序包含一个“网关”DataSnap REST 服务器,它是所有客户端的第一个访问点。根据客户端在请求中传递的用户名(基本身份验证),请求需要重定向到另一个 DataSnap 服务器。我的问题
我有一个 Tomcat 服务器和一个 Glassfish4 服务器。我的 Servlet 在 Tomcat 服务器上启动得很好,但在 Glassfish4 服务器上给我一个“HTTP Status 4
我在 vmware 上创建了一个 ubuntu 服务器。我用它作为文件服务器。如果我通过托管虚拟机的计算机进行连接,则可以访问它。我无法从同一网络上的其他计算机执行此操作。提前致谢! 最佳答案 首先确
如何重启 Rails 服务器?我从 开始 rails server -d 所以服务器是分离的 我知道的唯一方法就是去做ps 辅助 | grep rails 并 kill -9关于过程#但是像这样杀死进
我实际上正在尝试找到编写一个简单的 XMPP 服务器的最佳方法,或者找到一个占用空间非常小的服务器。我只关心XMPP的核心功能(状态、消息传递、群组消息传递)。目前还在学习 XMPP 协议(proto
我实际上正在尝试找到编写简单 XMPP 服务器的最佳方法,或者找到一个占用空间非常小的方法。我只关心 XMPP 的核心功能(统计、消息、组消息)。目前也在学习 XMPP 协议(protocol),所以
我们正在尝试从 Java JAX-RS 适配器访问 SOAP 1.1 Web 服务。 我们正在使用从 WSDL 生成的 SOAP 客户端。 但是当解码 SOAP 故障时,我们得到以下异常: ... C
目前,我和许多其他人正在多个平台(Windows、OS X 和可能的 Linux)上使用 Python HTTP 服务器。我们正在使用 Python HTTP 服务器来测试 JavaScript 游戏
我有一个连续运行的服务器程序(C#/.NET 2.0 on Linux with mono),我想从 PHP 脚本连接到它以在网站上显示状态信息。 目的是创建一个(某种)实时浏览器游戏(无 Flash
所以我有一个单页客户端应用程序。 正常流程: 应用程序 -> OAuth2 服务器 -> 应用程序 我们有自己的 OAuth2 服务器,因此人们可以登录应用程序并获取与用户实体关联的 access_t
我们刚刚将测试 Web 服务器从 Server 2008 升级到 Server 2012 R2。我们有一个部署我们网站的批处理脚本。当它将站点推送到服务器时,它现在失败了。奇怪的是,我可以使用相同的发
建议一些加载SpagoBI服务器的方法,我尝试了所有方法来解析spagobi服务器。在 Catalina 中,错误是 - * SEVERE: Unable to process Jar entry [
当我们点击应用程序服务器(apache tomcat)时,它会创建一个线程来处理我们的请求并与 tomcat 连接,建立连接,tomcat 创建另一个线程来处理请求并将其传递给连接,连接线程将其传递给
我是一名优秀的程序员,十分优秀!