File: client_advanced.py

package info (click to toggle)
python-autobahn 17.10.1%2Bdfsg1-7
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 4,452 kB
  • sloc: python: 22,598; javascript: 2,705; makefile: 497; sh: 3
file content (123 lines) | stat: -rw-r--r-- 4,682 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
###############################################################################
#
# The MIT License (MIT)
#
# Copyright (c) Crossbar.io Technologies GmbH
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.
#
###############################################################################

import sys

from twisted.internet import reactor
from twisted.python import log

from autobahn.twisted.websocket import WebSocketClientFactory, \
    WebSocketClientProtocol, \
    connectWS

from autobahn.websocket.compress import *


class EchoClientProtocol(WebSocketClientProtocol):

    def onConnect(self, response):
        print "WebSocket extensions in use: %s" % response.extensions

    def sendHello(self):
        self.sendMessage("Hello, world!" * 100)

    def onOpen(self):
        self.sendHello()

    def onMessage(self, payload, isBinary):
        if not isBinary:
            print("Text message received: {}".format(payload.decode('utf8')))
        reactor.callLater(1, self.sendHello)


if __name__ == '__main__':

    if len(sys.argv) < 2:
        print "Need the WebSocket server address, i.e. ws://127.0.0.1:9000"
        sys.exit(1)

    log.startLogging(sys.stdout)

    factory = WebSocketClientFactory(sys.argv[1])

    factory.protocol = EchoClientProtocol

    # Advanced usage: specify exact list of offers ("PMCE") we announce to server.
    #
    # Examples:
    #

    # this is just what the default constructor for PerMessageDeflateOffer
    # creates anyway
    offers1 = [PerMessageDeflateOffer(acceptNoContextTakeover=True,
                                      acceptMaxWindowBits=True,
                                      requestNoContextTakeover=False,
                                      request_max_window_bits=0)]

    # request the server to use a sliding window of 2^8 bytes
    offers2 = [PerMessageDeflateOffer(True, True, False, 8)]

    # request the server to use a sliding window of 2^8 bytes, but let the
    # server fall back to "standard" if server does not support the setting
    offers3 = [PerMessageDeflateOffer(True, True, False, 8),
               PerMessageDeflateOffer(True, True, False, 0)]

    # request "no context takeover", accept the same, but deny setting
    # a sliding window. no fallback!
    offers4 = [PerMessageDeflateOffer(True, False, True, 0)]

    # offer "permessage-snappy", "permessage-bzip2" and "permessage-deflate"
    # note that the first 2 are currently not even in an RFC draft
    #
    offers5 = []
    if 'permessage-snappy' in PERMESSAGE_COMPRESSION_EXTENSION:
        # this require snappy to be installed
        offers5.append(PerMessageSnappyOffer())
    offers5.append(PerMessageBzip2Offer(True, 1))
    offers5.append(PerMessageDeflateOffer(True, True, False, 12))

    # factory.setProtocolOptions(perMessageCompressionOffers = offers1)
    # factory.setProtocolOptions(perMessageCompressionOffers = offers2)
    # factory.setProtocolOptions(perMessageCompressionOffers = offers3)
    # factory.setProtocolOptions(perMessageCompressionOffers = offers4)
    factory.setProtocolOptions(perMessageCompressionOffers=offers5)

    # factory.setProtocolOptions(autoFragmentSize = 4)

    def accept(response):
        if isinstance(response, PerMessageDeflateResponse):
            return PerMessageDeflateResponseAccept(response)

        elif isinstance(response, PerMessageBzip2Response):
            return PerMessageBzip2ResponseAccept(response)

        elif isinstance(response, PerMessageSnappyResponse):
            return PerMessageSnappyResponseAccept(response)

    factory.setProtocolOptions(perMessageCompressionAccept=accept)

    connectWS(factory)
    reactor.run()