QWebSocket

Implements a TCP socket that talks the protocol. More

Inheritance diagram of PySide2.QtWebSockets.QWebSocket

Synopsis

Functions

Slots

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

  • def open (request)

  • def open (url)

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

Signals

Detailed Description

is a web technology providing full-duplex communications channels over a single TCP connection. The 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 protocol, as outlined in RFC 6455 .

Note

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

Warning

To generate masks, this implementation of 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 QWebSocket([origin=""[, version=QWebSocketProtocol.VersionLatest[, parent=None]]])
Parameters
  • parentQObject

  • origin – unicode

  • versionVersion

PySide2.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.

PySide2.QtWebSockets.QWebSocket.aboutToClose()
PySide2.QtWebSockets.QWebSocket.binaryFrameReceived(frame, isLastFrame)
Parameters
  • frameQByteArray

  • isLastFramebool

PySide2.QtWebSockets.QWebSocket.binaryMessageReceived(message)
Parameters

messageQByteArray

PySide2.QtWebSockets.QWebSocket.bytesToWrite()
Return type

qint64

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

PySide2.QtWebSockets.QWebSocket.bytesWritten(bytes)
Parameters

bytesqint64

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

  • reason – unicode

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.

PySide2.QtWebSockets.QWebSocket.closeCode()
Return type

CloseCode

Returns the code indicating why the socket was closed.

See also

CloseCode closeReason()

PySide2.QtWebSockets.QWebSocket.closeReason()
Return type

unicode

Returns the reason why the socket was closed.

See also

closeCode()

PySide2.QtWebSockets.QWebSocket.connected()
PySide2.QtWebSockets.QWebSocket.disconnected()
PySide2.QtWebSockets.QWebSocket.error()
Return type

SocketError

Returns the type of error that last occurred

See also

errorString()

PySide2.QtWebSockets.QWebSocket.error(error)
Parameters

errorSocketError

PySide2.QtWebSockets.QWebSocket.errorString()
Return type

unicode

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

See also

error()

PySide2.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.

PySide2.QtWebSockets.QWebSocket.isValid()
Return type

bool

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

PySide2.QtWebSockets.QWebSocket.localAddress()
Return type

QHostAddress

Returns the local address

PySide2.QtWebSockets.QWebSocket.localPort()
Return type

quint16

Returns the local port

PySide2.QtWebSockets.QWebSocket.maskGenerator()
Return type

QMaskGenerator

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

PySide2.QtWebSockets.QWebSocket.open(request)
Parameters

requestQNetworkRequest

PySide2.QtWebSockets.QWebSocket.open(url)
Parameters

urlQUrl

PySide2.QtWebSockets.QWebSocket.origin()
Return type

unicode

Returns the current origin.

PySide2.QtWebSockets.QWebSocket.pauseMode()
Return type

PauseModes

Returns the pause mode of this socket

See also

setPauseMode()

PySide2.QtWebSockets.QWebSocket.peerAddress()
Return type

QHostAddress

Returns the peer address

PySide2.QtWebSockets.QWebSocket.peerName()
Return type

unicode

Returns the

PySide2.QtWebSockets.QWebSocket.peerPort()
Return type

quint16

Returns the peerport

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

payloadQByteArray

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.

See also

pong()

PySide2.QtWebSockets.QWebSocket.pong(elapsedTime, payload)
Parameters
  • elapsedTimequint64

  • payloadQByteArray

PySide2.QtWebSockets.QWebSocket.preSharedKeyAuthenticationRequired(authenticator)
Parameters

authenticatorQSslPreSharedKeyAuthenticator

PySide2.QtWebSockets.QWebSocket.proxy()
Return type

QNetworkProxy

Returns the currently configured proxy

See also

setProxy()

PySide2.QtWebSockets.QWebSocket.proxyAuthenticationRequired(proxy, pAuthenticator)
Parameters
  • proxyQNetworkProxy

  • pAuthenticatorQAuthenticator

PySide2.QtWebSockets.QWebSocket.readBufferSize()
Return type

qint64

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

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

QNetworkRequest

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

PySide2.QtWebSockets.QWebSocket.requestUrl()
Return type

QUrl

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

PySide2.QtWebSockets.QWebSocket.resourceName()
Return type

unicode

Returns the name of the resource currently accessed.

PySide2.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.

PySide2.QtWebSockets.QWebSocket.sendBinaryMessage(data)
Parameters

dataQByteArray

Return type

qint64

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

PySide2.QtWebSockets.QWebSocket.sendTextMessage(message)
Parameters

message – unicode

Return type

qint64

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

PySide2.QtWebSockets.QWebSocket.setMaskGenerator(maskGenerator)
Parameters

maskGeneratorQMaskGenerator

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()

PySide2.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.

PySide2.QtWebSockets.QWebSocket.setProxy(networkProxy)
Parameters

networkProxyQNetworkProxy

Sets the proxy to networkProxy

See also

proxy()

PySide2.QtWebSockets.QWebSocket.setReadBufferSize(size)
Parameters

sizeqint64

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()

PySide2.QtWebSockets.QWebSocket.state()
Return type

SocketState

Returns the current state of the socket.

PySide2.QtWebSockets.QWebSocket.stateChanged(state)
Parameters

stateSocketState

PySide2.QtWebSockets.QWebSocket.textFrameReceived(frame, isLastFrame)
Parameters
  • frame – unicode

  • isLastFramebool

PySide2.QtWebSockets.QWebSocket.textMessageReceived(message)
Parameters

message – unicode

PySide2.QtWebSockets.QWebSocket.version()
Return type

Version

Returns the version the socket is currently using.