Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,8 @@ matrix:
env: TOXENV=lint
- python: 3.6
env: TOXENV=docs
# - python: "2.7"
# env: TOXENV=packaging
- python: "3.6"
env: TOXENV=packaging

install:
- "pip install -U pip setuptools"
Expand Down
19 changes: 19 additions & 0 deletions CHANGELOG.rst
Original file line number Diff line number Diff line change
@@ -1,6 +1,25 @@
0.13.0 Unreleased
-----------------

* Introduce a send method on the conenction which accepts the new
events. This requires the following usage changes,
connection.accept(subprotocol=subprotocol) -> connection.send(AcceptConnection(subprotocol=subprotocol))
connection.send_data(data) -> connection.send(Data(payload=payload))
connection.close(code) -> connection.send(CloseConnection(code=code))
connection.ping() -> connection.send(Ping())
connection.pong() -> connection.send(Pong())
* The Event structure is altered to allow for events to be sent and
received, this requires the following name changes in existing code,
ConnectionRequested -> Request
ConnectionEstablished -> AcceptConnection
ConnectionClosed -> CloseConnection
ConnectionFailed -> Fail
DataReceived -> Data
TextReceived -> TextMessage
BytesReceived -> BytesMessage
PingReceived -> Ping
PongReceived -> Pong

0.12.0 2018-09-23
-----------------

Expand Down
3 changes: 2 additions & 1 deletion MANIFEST.in
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
graft compliance
graft example
graft docs
graft test
graft wsproto
prune docs/build
prune compliance/autobahntestsuite-venv
prune compliance/reports
include README.rst LICENSE CHANGELOG.rst tox.ini
recursive-exclude * *.pyc *.pyo *.swo *.swp *.map *.DS_Store
global-exclude *.pyc *.pyo *.swo *.swp *.map *.yml *.DS_Store
20 changes: 12 additions & 8 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -42,13 +42,17 @@ handshake. To create a WebSocket client connection:
.. code-block:: python

from wsproto.connection import WSConnection, ConnectionType
ws = WSConnection(ConnectionType.CLIENT, host='echo.websocket.org', resource='/')
from wsproto.events import Request

ws = WSConnection(ConnectionType.CLIENT)
ws.send(Request(host='echo.websocket.org', target='/'))

To create a WebSocket server connection:

.. code-block:: python

from wsproto.connection import WSConnection, ConnectionType

ws = WSConnection(ConnectionType.SERVER)

Every time you send a message, or call a ping, or simply if you receive incoming
Expand All @@ -69,15 +73,15 @@ And wsproto will issue events if the data contains any WebSocket messages or sta
.. code-block:: python

for event in ws.events():
if isinstance(event, ConnectionRequested):
if isinstance(event, Request):
# only client connections get this event
ws.accept(event)
elif isinstance(event, ConnectionClosed):
ws.send(AcceptConnection())
elif isinstance(event, CloseConnection):
# guess nobody wants to talk to us any more...
elif isinstance(event, TextReceived):
print('We got a text!', event.data)
elif isinstance(event, TextReceived):
print('We got a text!', event.data)
elif isinstance(event, TextMessage):
print('We got text!', event.data)
elif isinstance(event, BytesMessage):
print('We got bytes!', event.data)

Take a look at our docs for a `full list of events
<https://wsproto.readthedocs.io/en/latest/api.html#events>`!
Expand Down
35 changes: 19 additions & 16 deletions compliance/test_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@
import socket

from wsproto.compat import PY2
from wsproto.connection import WSConnection, CLIENT, ConnectionEstablished, \
ConnectionClosed
from wsproto.events import TextReceived, DataReceived
from wsproto.connection import WSConnection, CLIENT
from wsproto.events import AcceptConnection, CloseConnection, Request, TextMessage, Data
from wsproto.extensions import PerMessageDeflate
from wsproto.frame_protocol import CloseReason

if PY2:
from urlparse import urlparse
Expand All @@ -22,7 +22,8 @@

def get_case_count(server):
uri = urlparse(server + '/getCaseCount')
connection = WSConnection(CLIENT, uri.netloc, uri.path)
connection = WSConnection(CLIENT)
connection.send(Request(host=uri.netloc, target=uri.path))
sock = socket.socket()
sock.connect((uri.hostname, uri.port or 80))

Expand All @@ -34,11 +35,11 @@ def get_case_count(server):
connection.receive_bytes(data)
data = ""
for event in connection.events():
if isinstance(event, TextReceived):
if isinstance(event, TextMessage):
data += event.data
if event.message_finished:
case_count = json.loads(data)
connection.close()
connection.send(CloseConnection(code=CloseReason.NORMAL_CLOSURE))
try:
sock.sendall(connection.bytes_to_send())
except CONNECTION_EXCEPTIONS:
Expand All @@ -49,9 +50,11 @@ def get_case_count(server):

def run_case(server, case, agent):
uri = urlparse(server + '/runCase?case=%d&agent=%s' % (case, agent))
connection = WSConnection(CLIENT,
uri.netloc, '%s?%s' % (uri.path, uri.query),
extensions=[PerMessageDeflate()])
connection = WSConnection(CLIENT)
connection.send(Request(
host=uri.netloc, target='%s?%s' % (uri.path, uri.query),
extensions=[PerMessageDeflate()],
))
sock = socket.socket()
sock.connect((uri.hostname, uri.port or 80))

Expand All @@ -65,9 +68,9 @@ def run_case(server, case, agent):
data = None
connection.receive_bytes(data or None)
for event in connection.events():
if isinstance(event, DataReceived):
connection.send_data(event.data, event.message_finished)
elif isinstance(event, ConnectionClosed):
if isinstance(event, Data):
connection.send(Data(data=event.data, message_finished=event.message_finished))
elif isinstance(event, CloseConnection):
closed = True
# else:
# print("??", event)
Expand All @@ -82,8 +85,8 @@ def run_case(server, case, agent):

def update_reports(server, agent):
uri = urlparse(server + '/updateReports?agent=%s' % agent)
connection = WSConnection(CLIENT,
uri.netloc, '%s?%s' % (uri.path, uri.query))
connection = WSConnection(CLIENT)
connection.send(Request(host=uri.netloc, target='%s?%s' % (uri.path, uri.query)))
sock = socket.socket()
sock.connect((uri.hostname, uri.port or 80))

Expand All @@ -94,8 +97,8 @@ def update_reports(server, agent):
data = sock.recv(65535)
connection.receive_bytes(data)
for event in connection.events():
if isinstance(event, ConnectionEstablished):
connection.close()
if isinstance(event, AcceptConnection):
connection.send(CloseConnection(code=CloseReason.NORMAL_CLOSURE))
sock.sendall(connection.bytes_to_send())
try:
sock.close()
Expand Down
17 changes: 8 additions & 9 deletions compliance/test_server.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
import select
import socket

from wsproto.connection import WSConnection, SERVER, ConnectionRequested, \
ConnectionClosed
from wsproto.events import DataReceived
from wsproto.connection import WSConnection, SERVER
from wsproto.events import AcceptConnection, CloseConnection, Data, Request
from wsproto.extensions import PerMessageDeflate

count = 0
Expand All @@ -12,7 +11,7 @@ def new_conn(sock):
global count
print("test_server.py received connection {}".format(count))
count += 1
ws = WSConnection(SERVER, extensions=[PerMessageDeflate()])
ws = WSConnection(SERVER)
closed = False
while not closed:
try:
Expand All @@ -23,11 +22,11 @@ def new_conn(sock):
ws.receive_bytes(data or None)

for event in ws.events():
if isinstance(event, ConnectionRequested):
ws.accept(event)
elif isinstance(event, DataReceived):
ws.send_data(event.data, event.message_finished)
elif isinstance(event, ConnectionClosed):
if isinstance(event, Request):
ws.send(AcceptConnection(extensions=[PerMessageDeflate()]))
elif isinstance(event, Data):
ws.send(Data(data=event.data, message_finished=event.message_finished))
elif isinstance(event, CloseConnection):
closed = True

if not data:
Expand Down
18 changes: 9 additions & 9 deletions docs/source/api.rst
Original file line number Diff line number Diff line change
Expand Up @@ -24,31 +24,31 @@ Events
.. autoclass:: wsproto.events.Event
:members:

.. autoclass:: wsproto.events.ConnectionRequested
.. autoclass:: wsproto.events.Request
:members:

.. autoclass:: wsproto.events.ConnectionEstablished
.. autoclass:: wsproto.events.AcceptConnection
:members:

.. autoclass:: wsproto.events.ConnectionClosed
.. autoclass:: wsproto.events.CloseConnection
:members:

.. autoclass:: wsproto.events.ConnectionFailed
.. autoclass:: wsproto.events.Fail
:members:

.. autoclass:: wsproto.events.DataReceived
.. autoclass:: wsproto.events.Data
:members:

.. autoclass:: wsproto.events.TextReceived
.. autoclass:: wsproto.events.TextMessage
:members:

.. autoclass:: wsproto.events.BytesReceived
.. autoclass:: wsproto.events.BytesMessage
:members:

.. autoclass:: wsproto.events.PingReceived
.. autoclass:: wsproto.events.Ping
:members:

.. autoclass:: wsproto.events.PongReceived
.. autoclass:: wsproto.events.Pong
:members:

Frame Protocol
Expand Down
23 changes: 12 additions & 11 deletions example/synchronous_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import sys

from wsproto.connection import ConnectionType, WSConnection
from wsproto.events import ConnectionEstablished, TextReceived, PongReceived
from wsproto.events import AcceptConnection, CloseConnection, Data, Ping, Pong, Request, TextMessage


RECEIVE_BYTES = 4096
Expand Down Expand Up @@ -49,7 +49,8 @@ def wsproto_demo(host, port):

# 1) Negotiate WebSocket opening handshake
print('Opening WebSocket')
ws = WSConnection(ConnectionType.CLIENT, host=host, resource='server')
ws = WSConnection(ConnectionType.CLIENT)
ws.send(Request(host=host, target='server'))

# events is a generator that yields websocket event objects. Usually you
# would say `for event in ws.events()`, but the synchronous nature of this
Expand All @@ -63,36 +64,36 @@ def wsproto_demo(host, port):
# a handshake, and we need to send it and wait for a response.
net_send_recv(ws, conn)
event = next(events)
if isinstance(event, ConnectionEstablished):
if isinstance(event, AcceptConnection):
print('WebSocket negotiation complete')
else:
raise Exception('Expected ConnectionEstablished event!')
raise Exception('Expected AcceptConnection event!')

# 2) Send a message and display response
message = "wsproto is great"
print('Sending message: {}'.format(message))
ws.send_data(message)
ws.send(Data(data=message))
net_send_recv(ws, conn)
event = next(events)
if isinstance(event, TextReceived):
if isinstance(event, TextMessage):
print('Received message: {}'.format(event.data))
else:
raise Exception('Expected TextReceived event!')
raise Exception('Expected TextMessage event!')

# 3) Send ping and display pong
payload = b"table tennis"
print('Sending ping: {}'.format(payload))
ws.ping(payload)
ws.send(Ping(payload=payload))
net_send_recv(ws, conn)
event = next(events)
if isinstance(event, PongReceived):
if isinstance(event, Pong):
print('Received pong: {}'.format(event.payload))
else:
raise Exception('Expected PongReceived event!')
raise Exception('Expected Pong event!')

# 4) Negotiate WebSocket closing handshake
print('Closing WebSocket')
ws.close(code=1000, reason='sample reason')
ws.send(CloseConnection(code=1000, reason='sample reason'))
# After sending the closing frame, we won't get any more events. The server
# should send a reply and then close the connection, so we need to receive
# twice:
Expand Down
15 changes: 7 additions & 8 deletions example/synchronous_server.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,7 @@
import sys

from wsproto.connection import ConnectionType, WSConnection
from wsproto.events import ConnectionClosed, ConnectionRequested, \
PingReceived, TextReceived
from wsproto.events import AcceptConnection, CloseConnection, Data, Ping, Request, TextMessage


MAX_CONNECTS = 5
Expand Down Expand Up @@ -79,20 +78,20 @@ def handle_connection(stream):
return

# 3) Handle event
if isinstance(event, ConnectionRequested):
if isinstance(event, Request):
# Negotiate new WebSocket connection
print('Accepting WebSocket upgrade')
ws.accept(event)
elif isinstance(event, ConnectionClosed):
ws.send(AcceptConnection())
elif isinstance(event, CloseConnection):
# Print log message and break out
print('Connection closed: code={}/{} reason={}'.format(
event.code.value, event.code.name, event.reason))
running = False
elif isinstance(event, TextReceived):
elif isinstance(event, TextMessage):
# Reverse text and send it back to wsproto
print('Received request and sending response')
ws.send_data(event.data[::-1])
elif isinstance(event, PingReceived):
ws.send(Data(data=event.data[::-1]))
elif isinstance(event, Ping):
# wsproto handles ping events for you by placing a pong frame in
# the outgoing buffer. You should not call pong() unless you want to
# send an unsolicited pong frame.
Expand Down
11 changes: 11 additions & 0 deletions setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,14 @@ source =
wsproto
.tox/*/lib/*/site-packages/wsproto
.tox/*/site-packages/wsproto

[isort]
combine_as_imports=True
force_grid_wrap=0
include_trailing_comma=True
known_first_party=wsproto, test
known_third_party=h11, pytest
line_length=88
multi_line_output=3
no_lines_before=LOCALFOLDER
order_by_type=False
Loading