QWebSocket

Implements a TCP socket that talks the WebSocket protocol. More

Inheritance diagram of PySide6.QtWebSockets.QWebSocket

Synopsis

Functions

Slots

  • def close ([closeCode=QWebSocketProtocol.CloseCodeNormal[, reason=””]])

  • def ignoreSslErrors ()

  • def open (request)

  • def open (url)

  • def ping ([payload=QByteArray()])

Signals

Static functions

Detailed Description

WebSockets is a web technology providing full-duplex communications channels over a single TCP connection. The WebSocket protocol was standardized by the IETF as RFC 6455 in 2011. QWebSocket can both be used in a client application and server application.

This class was modeled after QAbstractSocket .

QWebSocket currently does not support WebSocket Extensions and WebSocket Subprotocols .

QWebSocket only supports version 13 of the WebSocket protocol, as outlined in RFC 6455 .

Note

Some proxies do not understand certain HTTP headers used during a WebSocket handshake. In that case, non-secure WebSocket connections fail. The best way to mitigate against this problem is to use WebSocket over a secure connection.

Warning

To generate masks, this implementation of WebSockets uses the reasonably secure global() ->generate() function. For more information about the importance of good masking, see “Talking to Yourself for Fun and Profit” by Lin-Shung Huang et al . The best measure against attacks mentioned in the document above, is to use QWebSocket over a secure connection (wss://). In general, always be careful to not have 3rd party script access to a QWebSocket in your application.

See also

QAbstractSocket QTcpSocket QWebSocket client example

class PySide6.QtWebSockets.QWebSocket([origin=""[, version=QWebSocketProtocol.VersionLatest[, parent=None]]])
Parameters

Creates a new QWebSocket with the given origin, the version of the protocol to use and parent.

The origin of the client is as specified in RFC 6454 . (The origin is not required for non-web browser clients (see RFC 6455 )). The origin may not contain new line characters, otherwise the connection will be aborted immediately during the handshake phase.

Note

Currently only V13 ( RFC 6455 ) is supported

PySide6.QtWebSockets.QWebSocket.abort()

Aborts the current socket and resets the socket. Unlike close() , this function immediately closes the socket, discarding any pending data in the write buffer.

PySide6.QtWebSockets.QWebSocket.aboutToClose()
PySide6.QtWebSockets.QWebSocket.alertReceived(level, type, description)
Parameters
PySide6.QtWebSockets.QWebSocket.alertSent(level, type, description)
Parameters
PySide6.QtWebSockets.QWebSocket.binaryFrameReceived(frame, isLastFrame)
Parameters
PySide6.QtWebSockets.QWebSocket.binaryMessageReceived(message)
Parameters

messagePySide6.QtCore.QByteArray

PySide6.QtWebSockets.QWebSocket.bytesToWrite()
Return type

int

Returns the number of bytes that are waiting to be written. The bytes are written when control goes back to the event loop or when flush() is called.

See also

flush

PySide6.QtWebSockets.QWebSocket.bytesWritten(bytes)
Parameters

bytes – int

PySide6.QtWebSockets.QWebSocket.close([closeCode=QWebSocketProtocol.CloseCodeNormal[, reason=""]])
Parameters

Gracefully closes the socket with the given closeCode and reason.

Any data in the write buffer is flushed before the socket is closed. The closeCode is a CloseCode indicating the reason to close, and reason describes the reason of the closure more in detail. All control frames, including the Close frame, are limited to 125 bytes. Since two of these are used for closeCode the maximum length of reason is 123! If reason exceeds this limit it will be truncated.

PySide6.QtWebSockets.QWebSocket.closeCode()
Return type

CloseCode

Returns the code indicating why the socket was closed.

See also

CloseCode closeReason()

PySide6.QtWebSockets.QWebSocket.closeReason()
Return type

str

Returns the reason why the socket was closed.

See also

closeCode()

PySide6.QtWebSockets.QWebSocket.connected()
PySide6.QtWebSockets.QWebSocket.continueInterruptedHandshake()

If an application wants to conclude a handshake even after receiving handshakeInterruptedOnError() signal, it must call this function. This call must be done from a slot function attached to the signal. The signal-slot connection must be direct.

See also

handshakeInterruptedOnError() setHandshakeMustInterruptOnError()

PySide6.QtWebSockets.QWebSocket.disconnected()
PySide6.QtWebSockets.QWebSocket.error()
Return type

SocketError

Returns the type of error that last occurred

See also

errorString()

PySide6.QtWebSockets.QWebSocket.error(error)
Parameters

errorSocketError

PySide6.QtWebSockets.QWebSocket.errorString()
Return type

str

Returns a human-readable description of the last error that occurred

See also

error()

PySide6.QtWebSockets.QWebSocket.flush()
Return type

bool

This function writes as much as possible from the internal write buffer to the underlying network socket, without blocking. If any data was written, this function returns true; otherwise false is returned. Call this function if you need QWebSocket to start sending buffered data immediately. The number of bytes successfully written depends on the operating system. In most cases, you do not need to call this function, because QWebSocket will start sending data automatically once control goes back to the event loop.

PySide6.QtWebSockets.QWebSocket.handshakeInterruptedOnError(error)
Parameters

errorPySide6.QtNetwork.QSslError

PySide6.QtWebSockets.QWebSocket.ignoreSslErrors(errors)
Parameters

errors

This is an overloaded function.

This method tells QWebSocket to ignore the errors given in errors.

Note that you can set the expected certificate in the SSL error: If, for instance, you want to connect to a server that uses a self-signed certificate, consider the following snippet:

cert = QSslCertificate.fromPath("server-certificate.pem")
error = QSslError(QSslError.SelfSignedCertificate, cert.at(0))
expectedSslErrors = QList()
expectedSslErrors.append(error)
socket = QWebSocket()
socket.ignoreSslErrors(expectedSslErrors)
socket.open(QUrl(QStringLiteral("wss://myserver.at.home")))

Multiple calls to this function will replace the list of errors that were passed in previous calls. You can clear the list of errors you want to ignore by calling this function with an empty list.

See also

sslErrors()

PySide6.QtWebSockets.QWebSocket.ignoreSslErrors()

This slot tells QWebSocket to ignore errors during QWebSocket ‘s handshake phase and continue connecting. If you want to continue with the connection even if errors occur during the handshake phase, then you must call this slot, either from a slot connected to sslErrors() , or before the handshake phase. If you don’t call this slot, either in response to errors or before the handshake, the connection will be dropped after the sslErrors() signal has been emitted.

Warning

Be sure to always let the user inspect the errors reported by the sslErrors() signal, and only call this method upon confirmation from the user that proceeding is ok. If there are unexpected errors, the connection should be aborted. Calling this method without inspecting the actual errors will most likely pose a security risk for your application. Use it with great care!

See also

sslErrors() ignoreSslErrors() ignoreSslErrors()

PySide6.QtWebSockets.QWebSocket.isValid()
Return type

bool

Returns true if the socket is ready for reading and writing; otherwise returns false.

PySide6.QtWebSockets.QWebSocket.localAddress()
Return type

PySide6.QtNetwork.QHostAddress

Returns the local address

PySide6.QtWebSockets.QWebSocket.localPort()
Return type

quint16

Returns the local port

PySide6.QtWebSockets.QWebSocket.maskGenerator()
Return type

PySide6.QtWebSockets.QMaskGenerator

Returns the mask generator that is currently used by this QWebSocket .

PySide6.QtWebSockets.QWebSocket.maxAllowedIncomingFrameSize()
Return type

int

Returns the maximum allowed size of an incoming websocket frame.

PySide6.QtWebSockets.QWebSocket.maxAllowedIncomingMessageSize()
Return type

int

Returns the maximum allowed size of an incoming websocket message.

static PySide6.QtWebSockets.QWebSocket.maxIncomingFrameSize()
Return type

int

Returns the maximum supported size of an incoming websocket frame for this websocket implementation.

static PySide6.QtWebSockets.QWebSocket.maxIncomingMessageSize()
Return type

int

Returns the maximum supported size of an incoming websocket message for this websocket implementation.

static PySide6.QtWebSockets.QWebSocket.maxOutgoingFrameSize()
Return type

int

Returns the maximum supported size of an outgoing websocket frame for this websocket implementation.

PySide6.QtWebSockets.QWebSocket.open(request)
Parameters

requestPySide6.QtNetwork.QNetworkRequest

Opens a WebSocket connection using the given request.

The request url will be used to open the WebSocket connection. Headers present in the request will be sent to the server in the upgrade request, together with the ones needed for the websocket handshake.

PySide6.QtWebSockets.QWebSocket.open(url)
Parameters

urlPySide6.QtCore.QUrl

Opens a WebSocket connection using the given url.

If the url contains newline characters (\r\n), then the error signal will be emitted with ConnectionRefusedError as error type.

PySide6.QtWebSockets.QWebSocket.origin()
Return type

str

Returns the current origin.

PySide6.QtWebSockets.QWebSocket.outgoingFrameSize()
Return type

int

Returns the maximum size of an outgoing websocket frame.

PySide6.QtWebSockets.QWebSocket.pauseMode()
Return type

PauseModes

Returns the pause mode of this socket

See also

setPauseMode()

PySide6.QtWebSockets.QWebSocket.peerAddress()
Return type

PySide6.QtNetwork.QHostAddress

Returns the peer address

PySide6.QtWebSockets.QWebSocket.peerName()
Return type

str

Returns the

PySide6.QtWebSockets.QWebSocket.peerPort()
Return type

quint16

Returns the peerport

PySide6.QtWebSockets.QWebSocket.peerVerifyError(error)
Parameters

errorPySide6.QtNetwork.QSslError

PySide6.QtWebSockets.QWebSocket.ping([payload=QByteArray()])
Parameters

payloadPySide6.QtCore.QByteArray

Pings the server to indicate that the connection is still alive. Additional payload can be sent along the ping message.

The size of the payload cannot be bigger than 125. If it is larger, the payload is clipped to 125 bytes.

Note

QWebSocket and QWebSocketServer handles ping requests internally, which means they automatically send back a pong response to the peer.

See also

pong()

PySide6.QtWebSockets.QWebSocket.pong(elapsedTime, payload)
Parameters
PySide6.QtWebSockets.QWebSocket.preSharedKeyAuthenticationRequired(authenticator)
Parameters

authenticatorPySide6.QtNetwork.QSslPreSharedKeyAuthenticator

PySide6.QtWebSockets.QWebSocket.proxy()
Return type

PySide6.QtNetwork.QNetworkProxy

Returns the currently configured proxy

See also

setProxy()

PySide6.QtWebSockets.QWebSocket.proxyAuthenticationRequired(proxy, pAuthenticator)
Parameters
PySide6.QtWebSockets.QWebSocket.readBufferSize()
Return type

int

Returns the size in bytes of the readbuffer that is used by the socket.

PySide6.QtWebSockets.QWebSocket.readChannelFinished()
PySide6.QtWebSockets.QWebSocket.request()
Return type

PySide6.QtNetwork.QNetworkRequest

Returns the request that was or will be used to open this socket.

PySide6.QtWebSockets.QWebSocket.requestUrl()
Return type

PySide6.QtCore.QUrl

Returns the url the socket is connected to or will connect to.

PySide6.QtWebSockets.QWebSocket.resourceName()
Return type

str

Returns the name of the resource currently accessed.

PySide6.QtWebSockets.QWebSocket.resume()

Continues data transfer on the socket. This method should only be used after the socket has been set to pause upon notifications and a notification has been received. The only notification currently supported is sslErrors() . Calling this method if the socket is not paused results in undefined behavior.

PySide6.QtWebSockets.QWebSocket.sendBinaryMessage(data)
Parameters

dataPySide6.QtCore.QByteArray

Return type

int

Sends the given data over the socket as a binary message and returns the number of bytes actually sent.

PySide6.QtWebSockets.QWebSocket.sendTextMessage(message)
Parameters

message – str

Return type

int

Sends the given message over the socket as a text message and returns the number of bytes actually sent.

PySide6.QtWebSockets.QWebSocket.setMaskGenerator(maskGenerator)
Parameters

maskGeneratorPySide6.QtWebSockets.QMaskGenerator

Sets the generator to use for creating masks to maskGenerator. The default QWebSocket generator can be reset by supplying a nullptr. The mask generator can be changed at any time, even while the connection is open.

See also

maskGenerator()

PySide6.QtWebSockets.QWebSocket.setMaxAllowedIncomingFrameSize(maxAllowedIncomingFrameSize)
Parameters

maxAllowedIncomingFrameSize – int

Sets the maximum allowed size of an incoming websocket frame to maxAllowedIncomingFrameSize. If an incoming frame exceeds this limit, the peer gets disconnected. The accepted range is between 0 and maxIncomingFrameSize() , default is maxIncomingFrameSize() . The purpose of this function is to avoid exhausting virtual memory.

PySide6.QtWebSockets.QWebSocket.setMaxAllowedIncomingMessageSize(maxAllowedIncomingMessageSize)
Parameters

maxAllowedIncomingMessageSize – int

Sets the maximum allowed size of an incoming websocket message to maxAllowedIncomingMessageSize. If an incoming message exceeds this limit, the peer gets disconnected. The accepted range is between 0 and maxIncomingMessageSize() , default is maxIncomingMessageSize() . The purpose of this function is to avoid exhausting virtual memory.

PySide6.QtWebSockets.QWebSocket.setOutgoingFrameSize(outgoingFrameSize)
Parameters

outgoingFrameSize – int

Sets the maximum size of an outgoing websocket frame to outgoingFrameSize. The accepted range is between 0 and maxOutgoingFrameSize() , default is 512kB. The purpose of this function is to adapt to the maximum allowed frame size of the receiver.

PySide6.QtWebSockets.QWebSocket.setPauseMode(pauseMode)
Parameters

pauseModePauseModes

Controls whether to pause upon receiving a notification. The pauseMode parameter specifies the conditions in which the socket should be paused.

The only notification currently supported is sslErrors() . If set to PauseOnSslErrors, data transfer on the socket will be paused and needs to be enabled explicitly again by calling resume() . By default, this option is set to PauseNever. This option must be called before connecting to the server, otherwise it will result in undefined behavior.

PySide6.QtWebSockets.QWebSocket.setProxy(networkProxy)
Parameters

networkProxyPySide6.QtNetwork.QNetworkProxy

Sets the proxy to networkProxy

See also

proxy()

PySide6.QtWebSockets.QWebSocket.setReadBufferSize(size)
Parameters

size – int

Sets the size of QWebSocket ‘s internal read buffer to be size bytes.

If the buffer size is limited to a certain size, QWebSocket won’t buffer more than this size of data. Exceptionally, a buffer size of 0 means that the read buffer is unlimited and all incoming data is buffered. This is the default. This option is useful if you only read the data at certain points in time (for example, in a real-time streaming application) or if you want to protect your socket against receiving too much data, which may eventually cause your application to run out of memory.

See also

readBufferSize()

PySide6.QtWebSockets.QWebSocket.setSslConfiguration(sslConfiguration)
Parameters

sslConfigurationPySide6.QtNetwork.QSslConfiguration

Sets the socket’s SSL configuration to be the contents of sslConfiguration.

This function sets the local certificate, the ciphers, the private key and the CA certificates to those stored in sslConfiguration. It is not possible to set the SSL-state related fields.

PySide6.QtWebSockets.QWebSocket.sslConfiguration()
Return type

PySide6.QtNetwork.QSslConfiguration

Returns the socket’s SSL configuration state. The default SSL configuration of a socket is to use the default ciphers, default CA certificates, no local private key or certificate. The SSL configuration also contains fields that can change with time without notice.

PySide6.QtWebSockets.QWebSocket.sslErrors(errors)
Parameters

errors

PySide6.QtWebSockets.QWebSocket.state()
Return type

SocketState

Returns the current state of the socket.

PySide6.QtWebSockets.QWebSocket.stateChanged(state)
Parameters

stateSocketState

PySide6.QtWebSockets.QWebSocket.textFrameReceived(frame, isLastFrame)
Parameters
  • frame – str

  • isLastFrame – bool

PySide6.QtWebSockets.QWebSocket.textMessageReceived(message)
Parameters

message – str

PySide6.QtWebSockets.QWebSocket.version()
Return type

Version

Returns the version the socket is currently using.