本文整理匯總了Python中PyQt5.QtNetwork.QTcpSocket方法的典型用法代碼示例。如果您正苦於以下問題:Python QtNetwork.QTcpSocket方法的具體用法?Python QtNetwork.QTcpSocket怎麽用?Python QtNetwork.QTcpSocket使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類PyQt5.QtNetwork
的用法示例。
在下文中一共展示了QtNetwork.QTcpSocket方法的9個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: __init__
# 需要導入模塊: from PyQt5 import QtNetwork [as 別名]
# 或者: from PyQt5.QtNetwork import QTcpSocket [as 別名]
def __init__(self, socket: QtNetwork.QTcpSocket = None):
"""
Initializes the extended TCP socket. Either wraps around an existing socket or creates its own and resets
the number of bytes written.
:param socket: An already existing socket or None if none is given.
"""
super().__init__()
# new QTcpSocket() if none is given
if socket is not None:
self.socket = socket
else:
self.socket = QtNetwork.QTcpSocket()
# some wiring, new data is handled by _receive()
self.socket.readyRead.connect(self._receive)
self.socket.error.connect(self.error)
self.socket.connected.connect(self.connected)
self.socket.disconnected.connect(self.disconnected)
self.socket.bytesWritten.connect(self.count_bytes_written)
self.bytes_written = 0
示例2: connect
# 需要導入模塊: from PyQt5 import QtNetwork [as 別名]
# 或者: from PyQt5.QtNetwork import QTcpSocket [as 別名]
def connect(self, remoteAddress, port):
"""Establishes a session with the debugger"""
self.socket = QTcpSocket()
if remoteAddress is None:
self.socket.connectToHost(QHostAddress.LocalHost, port)
else:
self.socket.connectToHost(remoteAddress, port)
if not self.socket.waitForConnected(1000):
raise Exception('Cannot connect to the IDE')
self.socket.setSocketOption(QAbstractSocket.KeepAliveOption, 1)
self.socket.setSocketOption(QAbstractSocket.LowDelayOption, 1)
self.socket.disconnected.connect(self.__onDisconnected)
示例3: connect
# 需要導入模塊: from PyQt5 import QtNetwork [as 別名]
# 或者: from PyQt5.QtNetwork import QTcpSocket [as 別名]
def connect(self, remoteAddress, port):
"""Establishes a connection with the IDE"""
self.__socket = QTcpSocket()
if remoteAddress is None:
self.__socket.connectToHost(QHostAddress.LocalHost, port)
else:
self.__socket.connectToHost(remoteAddress, port)
if not self.__socket.waitForConnected(1000):
raise Exception('Cannot connect to the IDE')
self.__socket.setSocketOption(QAbstractSocket.KeepAliveOption, 1)
self.__socket.setSocketOption(QAbstractSocket.LowDelayOption, 1)
self.__socket.disconnected.connect(self.__onDisconnected)
示例4: __init__
# 需要導入模塊: from PyQt5 import QtNetwork [as 別名]
# 或者: from PyQt5.QtNetwork import QTcpSocket [as 別名]
def __init__(self, socket: QtNetwork.QTcpSocket = None):
"""
We start with an empty channels list.
:param socket: A socket if there is one existing already.
"""
super().__init__(socket)
self.received.connect(self._process)
self.channels = {}
示例5: _new_client
# 需要導入模塊: from PyQt5 import QtNetwork [as 別名]
# 或者: from PyQt5.QtNetwork import QTcpSocket [as 別名]
def _new_client(self, socket: QtNetwork.QTcpSocket):
"""
A new connection (QTCPPSocket) to the server occurred. Give it an id and add some general receivers to the new
server client (wrap the socket into a NetworkClient). Add the new server client to the internal client list.
Not intended for outside use.
:param socket: The socket for the new connection
"""
# wrap into a NetworkClient
client = ServerNetworkClient(socket)
# give it a new id
while True:
# theoretically this could take forever, practically only if we have 1e6 clients already
new_id = random.randint(0, 1e6)
if not any([new_id == client.client_id for client in self.server_clients]):
# not any == none
break
# noinspection PyUnboundLocalVariable
client.client_id = new_id
logger.info('new client with id {}'.format(new_id))
# add some general channels and receivers
# TODO the receivers should be in another module eventually
client.connect_to_channel(constants.C.LOBBY, self._lobby_messages)
client.connect_to_channel(constants.C.GENERAL, general_messages)
# TODO only if localhost connection add the system channel
client.connect_to_channel(constants.C.SYSTEM, self._system_messages)
# chat message system, handled by a single central routine
client.connect_to_channel(constants.C.CHAT, self._chat_system)
# finally add to list of clients
self.server_clients.append(client)
示例6: _new_connection
# 需要導入模塊: from PyQt5 import QtNetwork [as 別名]
# 或者: from PyQt5.QtNetwork import QTcpSocket [as 別名]
def _new_connection(self):
"""
Called by the newConnection signal of the QTCPServer. Not intended for outside use.
Zero or more new clients might be available, emit new_client signal for each of them.
"""
logger.info('new connection on server')
while self.tcp_server.hasPendingConnections():
# returns a new QTcpSocket
socket = self.tcp_server.nextPendingConnection()
# emit signal
self.new_client.emit(socket)
示例7: connect
# 需要導入模塊: from PyQt5 import QtNetwork [as 別名]
# 或者: from PyQt5.QtNetwork import QTcpSocket [as 別名]
def connect(self):
self.socket = qtnetwork.QTcpSocket()
# set TCP_NODELAY (disable Nagle's algorithm)
self.socket.setSocketOption(
qtnetwork.QAbstractSocket.LowDelayOption, True)
self.socket.connectToHost(self.host, self.port)
示例8: __init__
# 需要導入模塊: from PyQt5 import QtNetwork [as 別名]
# 或者: from PyQt5.QtNetwork import QTcpSocket [as 別名]
def __init__(self, username, recipient):
super().__init__()
self.username = username
self.recipient = recipient
self.listener = qtn.QTcpServer()
self.listener.listen(qtn.QHostAddress.Any, self.port)
self.listener.newConnection.connect(self.on_connection)
self.listener.acceptError.connect(self.on_error)
self.connections = []
self.client_socket = qtn.QTcpSocket()
self.client_socket.error.connect(self.on_error)
示例9: doConnect
# 需要導入模塊: from PyQt5 import QtNetwork [as 別名]
# 或者: from PyQt5.QtNetwork import QTcpSocket [as 別名]
def doConnect(self):
"""連接服務器"""
self.buttonConnect.setEnabled(False)
self._timer.stop()
self._clearConn()
self.browserResult.append('正在連接服務器')
# 連接控製小車的服務器
self._connCar = QTcpSocket(self)
self._connCar.connected.connect(self.onConnected) # 綁定連接成功信號
self._connCar.disconnected.connect(self.onDisconnected) # 綁定連接丟失信號
self._connCar.readyRead.connect(self.onReadyRead) # 準備讀取信號
self._connCar.error.connect(self.onError) # 連接錯誤信號
self._connCar.connectToHost(self.HOST, self.PORT)