Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
W
wspy
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Analytics
Analytics
CI / CD
Repository
Value Stream
Wiki
Wiki
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Taddeüs Kroes
wspy
Commits
6641c0c4
Commit
6641c0c4
authored
Jul 02, 2013
by
Taddeüs Kroes
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Code cleanup
parent
bd6d116d
Changes
4
Show whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
23 additions
and
19 deletions
+23
-19
Makefile
Makefile
+3
-3
connection.py
connection.py
+8
-4
frame.py
frame.py
+2
-2
message.py
message.py
+10
-10
No files found.
Makefile
View file @
6641c0c4
.PHONY
:
test
clean
.PHONY
:
check
clean
test
:
check
:
@
python test.py
clean
:
rm
`
find
-name
\*
.pyc
`
find
-name
\*
.pyc
-delete
connection.py
View file @
6641c0c4
import
struct
from
frame
import
ControlFrame
,
OPCODE_CLOSE
,
OPCODE_PING
,
OPCODE_PONG
from
frame
import
ControlFrame
,
OPCODE_CLOSE
,
OPCODE_PING
,
OPCODE_PONG
,
\
OPCODE_CONTINUATION
from
message
import
create_message
from
exceptions
import
SocketClosed
,
PingError
...
...
@@ -43,8 +44,8 @@ class Connection(object):
"""
Receive a message. A message may consist of multiple (ordered) data
frames. A control frame may be delivered at any time, also when
expecting the next
data frame of a fragmented message. These control
frames are handled immediately bu
handle_control_frame().
expecting the next
continuation frame of a fragmented message. These
control frames are handled immediately by
handle_control_frame().
"""
fragments
=
[]
...
...
@@ -57,10 +58,13 @@ class Connection(object):
# No more receiving data after a close message
if
frame
.
opcode
==
OPCODE_CLOSE
:
break
elif
len
(
fragments
)
and
frame
.
opcode
!=
OPCODE_CONTINUATION
:
raise
ValueError
(
'expected continuation/control frame, got %s '
'instead'
%
frame
)
else
:
fragments
.
append
(
frame
)
payload
=
''
.
join
(
[
f
.
payload
for
f
in
fragments
]
)
payload
=
''
.
join
(
f
.
payload
for
f
in
fragments
)
return
create_message
(
fragments
[
0
].
opcode
,
payload
)
def
handle_control_frame
(
self
,
frame
):
...
...
frame.py
View file @
6641c0c4
...
...
@@ -127,7 +127,7 @@ class Frame(object):
"""
frames
=
[]
for
start
in
range
(
0
,
len
(
self
.
payload
),
fragment_size
):
for
start
in
x
range
(
0
,
len
(
self
.
payload
),
fragment_size
):
payload
=
self
.
payload
[
start
:
start
+
fragment_size
]
frames
.
append
(
Frame
(
OPCODE_CONTINUATION
,
payload
,
mask
=
mask
,
final
=
False
))
...
...
@@ -149,7 +149,7 @@ class Frame(object):
class
ControlFrame
(
Frame
):
"""
A
C
ontrol frame is a frame with an opcode OPCODE_CLOSE, OPCODE_PING or
A
c
ontrol frame is a frame with an opcode OPCODE_CLOSE, OPCODE_PING or
OPCODE_PONG. These frames must be handled as defined by RFC 6455, and
"""
def
fragment
(
self
,
fragment_size
,
mask
=
False
):
...
...
message.py
View file @
6641c0c4
...
...
@@ -33,21 +33,21 @@ class BinaryMessage(Message):
class
JSONMessage
(
TextMessage
):
def
__init__
(
self
,
d
ictionary
,
**
kwargs
):
def
__init__
(
self
,
d
ata
,
**
kwargs
):
self
.
data
=
{}
self
.
data
.
extend
(
dictionary
)
self
.
data
.
extend
(
kwargs
)
self
.
data
.
update
(
data
,
**
kwargs
)
super
(
JSONMessage
,
self
).
__init__
(
json
.
dumps
(
self
.
data
))
OPCODE_CLASS_MAP
=
{
OPCODE_TEXT
:
TextMessage
,
OPCODE_BINARY
:
BinaryMessage
,
}
@
classmethod
def
decode
(
cls
,
payload
):
return
cls
(
json
.
loads
(
payload
))
def
create_message
(
opcode
,
payload
):
if
opcode
in
OPCODE_CLASS_MAP
:
return
OPCODE_CLASS_MAP
[
opcode
](
payload
)
if
opcode
==
OPCODE_TEXT
:
return
TextMessage
(
payload
)
if
opcode
==
OPCODE_BINARY
:
return
BinaryMessage
(
payload
)
return
Message
(
opcode
,
payload
)
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment