本文整理汇总了Python中PyQt5.QtNetwork.QLocalServer.errorString方法的典型用法代码示例。如果您正苦于以下问题:Python QLocalServer.errorString方法的具体用法?Python QLocalServer.errorString怎么用?Python QLocalServer.errorString使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类PyQt5.QtNetwork.QLocalServer
的用法示例。
在下文中一共展示了QLocalServer.errorString方法的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: Server
# 需要导入模块: from PyQt5.QtNetwork import QLocalServer [as 别名]
# 或者: from PyQt5.QtNetwork.QLocalServer import errorString [as 别名]
class Server(QDialog):
def __init__(self, parent=None):
super(Server, self).__init__(parent)
statusLabel = QLabel()
statusLabel.setWordWrap(True)
quitButton = QPushButton("Quit")
quitButton.setAutoDefault(False)
self.fortunes = (
"You've been leading a dog's life. Stay off the furniture.",
"You've got to think about tomorrow.",
"You will be surprised by a loud noise.",
"You will feel hungry again in another hour.",
"You might have mail.",
"You cannot kill time without injuring eternity.",
"Computers are not intelligent. They only think they are.",
)
self.server = QLocalServer()
if not self.server.listen('fortune'):
QMessageBox.critical(self, "Fortune Server",
"Unable to start the server: %s." % self.server.errorString())
self.close()
return
statusLabel.setText("The server is running.\nRun the Fortune Client "
"example now.")
quitButton.clicked.connect(self.close)
self.server.newConnection.connect(self.sendFortune)
buttonLayout = QHBoxLayout()
buttonLayout.addStretch(1)
buttonLayout.addWidget(quitButton)
buttonLayout.addStretch(1)
mainLayout = QVBoxLayout()
mainLayout.addWidget(statusLabel)
mainLayout.addLayout(buttonLayout)
self.setLayout(mainLayout)
self.setWindowTitle("Fortune Server")
def sendFortune(self):
block = QByteArray()
out = QDataStream(block, QIODevice.WriteOnly)
out.setVersion(QDataStream.Qt_4_0)
out.writeUInt16(0)
out.writeQString(random.choice(self.fortunes))
out.device().seek(0)
out.writeUInt16(block.size() - 2)
clientConnection = self.server.nextPendingConnection()
clientConnection.disconnected.connect(clientConnection.deleteLater)
clientConnection.write(block)
clientConnection.flush()
clientConnection.disconnectFromServer()
示例2: test_socket_options_address_in_use_problem
# 需要导入模块: from PyQt5.QtNetwork import QLocalServer [as 别名]
# 或者: from PyQt5.QtNetwork.QLocalServer import errorString [as 别名]
def test_socket_options_address_in_use_problem(qlocalserver, short_tmpdir):
"""Qt seems to ignore AddressInUseError when using socketOptions.
With this test we verify this bug still exists. If it fails, we can
probably start using setSocketOptions again.
"""
servername = str(short_tmpdir / 'x')
s1 = QLocalServer()
ok = s1.listen(servername)
assert ok
s2 = QLocalServer()
s2.setSocketOptions(QLocalServer.UserAccessOption)
ok = s2.listen(servername)
print(s2.errorString())
# We actually would expect ok == False here - but we want the test to fail
# when the Qt bug is fixed.
assert ok
示例3: Server
# 需要导入模块: from PyQt5.QtNetwork import QLocalServer [as 别名]
# 或者: from PyQt5.QtNetwork.QLocalServer import errorString [as 别名]
class Server(QObject):
dataReceived = pyqtSignal(list)
quit = pyqtSignal()
def __init__(self):
super().__init__()
self.conn = None
self.server = None
def create(self, name=piony.G_SOCKET_NAME):
QLocalServer.removeServer(name)
self.server = QLocalServer()
if not self.server.listen(name):
print("Error: server -- unable to start: {}."
.format(self.server.errorString()))
self.quit.emit()
self.server.newConnection.connect(self.notify)
def close(self):
self.server.close()
def notify(self):
logger.info("1 new conn")
# WARNING: when multiple connections, each will overwrite previous!
self.conn = self.server.nextPendingConnection()
self.conn.readyRead.connect(self.receiveData)
self.conn.disconnected.connect(self.conn.deleteLater)
def receiveData(self):
logger.info("waits for data")
ins = QDataStream(self.conn)
ins.setVersion(QDataStream.Qt_5_0)
if ins.atEnd():
return
argv = ins.readQVariant()
logger.info("reads '%s'", str(argv))
# Must be setted up on 'show' action. Move from beginning to appropriate.
action.search_dst_window()
self.dataReceived.emit(argv)
示例4: IPCServer
# 需要导入模块: from PyQt5.QtNetwork import QLocalServer [as 别名]
# 或者: from PyQt5.QtNetwork.QLocalServer import errorString [as 别名]
class IPCServer(QObject):
"""IPC server to which clients connect to.
Attributes:
ignored: Whether requests are ignored (in exception hook).
_timer: A timer to handle timeouts.
_server: A QLocalServer to accept new connections.
_socket: The QLocalSocket we're currently connected to.
"""
def __init__(self, parent=None):
"""Start the IPC server and listen to commands."""
super().__init__(parent)
self.ignored = False
self._remove_server()
self._timer = usertypes.Timer(self, 'ipc-timeout')
self._timer.setInterval(READ_TIMEOUT)
self._timer.timeout.connect(self.on_timeout)
self._server = QLocalServer(self)
ok = self._server.listen(SOCKETNAME)
if not ok:
raise IPCError("Error while listening to IPC server: {} "
"(error {})".format(self._server.errorString(),
self._server.serverError()))
self._server.newConnection.connect(self.handle_connection)
self._socket = None
def _remove_server(self):
"""Remove an existing server."""
ok = QLocalServer.removeServer(SOCKETNAME)
if not ok:
raise IPCError("Error while removing server {}!".format(
SOCKETNAME))
@pyqtSlot(int)
def on_error(self, error):
"""Convenience method which calls _socket_error on an error."""
self._timer.stop()
log.ipc.debug("Socket error {}: {}".format(
self._socket.error(), self._socket.errorString()))
if error != QLocalSocket.PeerClosedError:
_socket_error("handling IPC connection", self._socket)
@pyqtSlot()
def handle_connection(self):
"""Handle a new connection to the server."""
if self.ignored:
return
if self._socket is not None:
log.ipc.debug("Got new connection but ignoring it because we're "
"still handling another one.")
return
socket = self._server.nextPendingConnection()
if socket is None:
log.ipc.debug("No new connection to handle.")
return
log.ipc.debug("Client connected.")
self._timer.start()
self._socket = socket
socket.readyRead.connect(self.on_ready_read)
if socket.canReadLine():
log.ipc.debug("We can read a line immediately.")
self.on_ready_read()
socket.error.connect(self.on_error)
if socket.error() not in (QLocalSocket.UnknownSocketError,
QLocalSocket.PeerClosedError):
log.ipc.debug("We got an error immediately.")
self.on_error(socket.error())
socket.disconnected.connect(self.on_disconnected)
if socket.state() == QLocalSocket.UnconnectedState:
log.ipc.debug("Socket was disconnected immediately.")
self.on_disconnected()
@pyqtSlot()
def on_disconnected(self):
"""Clean up socket when the client disconnected."""
log.ipc.debug("Client disconnected.")
self._timer.stop()
self._socket.deleteLater()
self._socket = None
# Maybe another connection is waiting.
self.handle_connection()
@pyqtSlot()
def on_ready_read(self):
"""Read json data from the client."""
if self._socket is None:
# this happened once and I don't know why
log.ipc.warn("In on_ready_read with None socket!")
return
self._timer.start()
while self._socket is not None and self._socket.canReadLine():
data = bytes(self._socket.readLine())
log.ipc.debug("Read from socket: {}".format(data))
try:
decoded = data.decode('utf-8')
except UnicodeDecodeError:
log.ipc.error("Ignoring invalid IPC data.")
log.ipc.debug("invalid data: {}".format(
#.........这里部分代码省略.........