本文整理汇总了Python中PyQt5.QtNetwork.QLocalSocket.write方法的典型用法代码示例。如果您正苦于以下问题:Python QLocalSocket.write方法的具体用法?Python QLocalSocket.write怎么用?Python QLocalSocket.write使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类PyQt5.QtNetwork.QLocalSocket
的用法示例。
在下文中一共展示了QLocalSocket.write方法的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: _send_to_running_instance
# 需要导入模块: from PyQt5.QtNetwork import QLocalSocket [as 别名]
# 或者: from PyQt5.QtNetwork.QLocalSocket import write [as 别名]
def _send_to_running_instance(self, payload: bytes, pid: int) -> None:
from PyQt5.QtCore import QByteArray
from PyQt5.QtNetwork import QLocalSocket
named_pipe = f"{BUNDLE_IDENTIFIER}.protocol.{pid}"
log.debug(
f"Opening a local socket to the running instance on {named_pipe} "
f"(payload={self.redact_payload(payload)})"
)
client = QLocalSocket()
try:
client.connectToServer(named_pipe)
if not client.waitForConnected():
log.error(f"Unable to open client socket: {client.errorString()}")
return
client.write(QByteArray(payload))
client.waitForBytesWritten()
client.disconnectFromServer()
if client.state() == QLocalSocket.ConnectedState:
client.waitForDisconnected()
finally:
del client
log.debug("Successfully closed client socket")
示例2: __init__
# 需要导入模块: from PyQt5.QtNetwork import QLocalSocket [as 别名]
# 或者: from PyQt5.QtNetwork.QLocalSocket import write [as 别名]
class Client:
def __init__(self):
self.socket = QLocalSocket()
self.socket.setServerName(piony.G_SOCKET_NAME)
self.socket.error.connect(self.displayError)
self.socket.disconnected.connect(self.socket.deleteLater)
def _log_(self, text, *args):
logger.info(self.__class__.__qualname__ + ': ' + text, *args)
def connect(self):
self.socket.abort()
self._log_("connection attempt")
self.socket.connectToServer()
def send(self, argv):
data = QByteArray()
out = QDataStream(data, QIODevice.WriteOnly)
out.setVersion(QDataStream.Qt_5_0)
out.writeQVariant(argv)
self._log_("writes: %s", str(data))
self.socket.write(data)
self.socket.flush()
self.socket.disconnectFromServer()
def displayError(self, err):
msg = {
QLocalSocket.ServerNotFoundError:
"The host was not found. Check the host name and port.",
QLocalSocket.ConnectionRefusedError:
"The connection was refused by the peer. "
"Check server is running, it's host and port.",
QLocalSocket.PeerClosedError:
"Peer was closed", # None,
}.get(err, "Client error: {}.".format(self.socket.errorString()))
self._log_(msg)
示例3: startClient
# 需要导入模块: from PyQt5.QtNetwork import QLocalSocket [as 别名]
# 或者: from PyQt5.QtNetwork.QLocalSocket import write [as 别名]
def startClient(self) -> bool:
Logger.log("i", "Checking for the presence of an ready running Cura instance.")
single_instance_socket = QLocalSocket(self._application)
Logger.log("d", "Full single instance server name: %s", single_instance_socket.fullServerName())
single_instance_socket.connectToServer("ultimaker-cura")
single_instance_socket.waitForConnected(msecs = 3000) # wait for 3 seconds
if single_instance_socket.state() != QLocalSocket.ConnectedState:
return False
# We only send the files that need to be opened.
if not self._files_to_open:
Logger.log("i", "No file need to be opened, do nothing.")
return True
if single_instance_socket.state() == QLocalSocket.ConnectedState:
Logger.log("i", "Connection has been made to the single-instance Cura socket.")
# Protocol is one line of JSON terminated with a carriage return.
# "command" field is required and holds the name of the command to execute.
# Other fields depend on the command.
payload = {"command": "clear-all"}
single_instance_socket.write(bytes(json.dumps(payload) + "\n", encoding = "ascii"))
payload = {"command": "focus"}
single_instance_socket.write(bytes(json.dumps(payload) + "\n", encoding = "ascii"))
for filename in self._files_to_open:
payload = {"command": "open", "filePath": os.path.abspath(filename)}
single_instance_socket.write(bytes(json.dumps(payload) + "\n", encoding = "ascii"))
payload = {"command": "close-connection"}
single_instance_socket.write(bytes(json.dumps(payload) + "\n", encoding = "ascii"))
single_instance_socket.flush()
single_instance_socket.waitForDisconnected()
return True
示例4: SingleApplicationClient
# 需要导入模块: from PyQt5.QtNetwork import QLocalSocket [as 别名]
# 或者: from PyQt5.QtNetwork.QLocalSocket import write [as 别名]
class SingleApplicationClient(object):
"""
Class implementing the single application client base class.
"""
def __init__(self, name):
"""
Constructor
@param name name of the local server to connect to (string)
"""
self.name = name
self.connected = False
def connect(self):
"""
Public method to connect the single application client to its server.
@return value indicating success or an error number. Value is one of:
<table>
<tr><td>0</td><td>No application is running</td></tr>
<tr><td>1</td><td>Application is already running</td></tr>
</table>
"""
self.sock = QLocalSocket()
self.sock.connectToServer(self.name)
if self.sock.waitForConnected(10000):
self.connected = True
return 1
else:
err = self.sock.error()
if err == QLocalSocket.ServerNotFoundError:
return 0
else:
return -err
def disconnect(self):
"""
Public method to disconnect from the Single Appliocation server.
"""
self.sock.disconnectFromServer()
self.connected = False
def processArgs(self, args):
"""
Public method to process the command line args passed to the UI.
<b>Note</b>: This method must be overridden by subclasses.
@param args command line args (list of strings)
@exception RuntimeError raised to indicate that this method must be
implemented by a subclass
"""
raise RuntimeError("'processArgs' must be overridden")
def sendCommand(self, cmd):
"""
Public method to send the command to the application server.
@param cmd command to be sent (string)
"""
if self.connected:
self.sock.write(cmd)
self.sock.flush()
def errstr(self):
"""
Public method to return a meaningful error string for the last error.
@return error string for the last error (string)
"""
return self.sock.errorString()