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
7b56e728
Commit
7b56e728
authored
Nov 07, 2012
by
Taddeüs Kroes
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Server now closes all client connections after keyboard interrupt
parent
9f7b6c86
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
19 additions
and
16 deletions
+19
-16
server.py
server.py
+15
-2
websocket.py
websocket.py
+4
-14
No files found.
server.py
View file @
7b56e728
import
socket
import
logging
from
traceback
import
format_exc
from
threading
import
Thread
from
websocket
import
WebSocket
from
exceptions
import
InvalidRequest
from
frame
import
CLOSE_NORMAL
class
Server
(
object
):
...
...
@@ -26,10 +28,14 @@ class Server(object):
try
:
sock
,
address
=
self
.
sock
.
accept
()
client
=
Client
(
self
,
sock
,
address
)
client
.
handshake
()
client
.
server_handshake
()
self
.
clients
.
append
(
client
)
logging
.
info
(
'Registered client %s'
,
client
)
client
.
run_threaded
()
thread
=
Thread
(
target
=
client
.
receive_forever
)
thread
.
daemon
=
True
thread
.
start
()
except
InvalidRequest
as
e
:
logging
.
error
(
'Invalid request: %s'
,
e
.
message
)
except
KeyboardInterrupt
:
...
...
@@ -38,6 +44,12 @@ class Server(object):
except
Exception
as
e
:
logging
.
error
(
format_exc
(
e
))
self
.
quit_gracefully
()
def
quit_gracefully
(
self
):
for
client
in
self
.
clients
:
client
.
close
(
CLOSE_NORMAL
)
def
remove_client
(
self
,
client
,
code
,
reason
):
self
.
clients
.
remove
(
client
)
self
.
onclose
(
client
,
code
,
reason
)
...
...
@@ -71,6 +83,7 @@ class Client(WebSocket):
super
(
Client
,
self
).
__init__
(
sock
)
self
.
server
=
server
self
.
address
=
address
self
.
send_lock
=
Lock
()
def
onopen
(
self
):
self
.
server
.
onopen
(
self
)
...
...
websocket.py
View file @
7b56e728
import
re
import
struct
from
hashlib
import
sha1
from
threading
import
Thread
from
frame
import
ControlFrame
,
receive_fragments
,
receive_frame
,
\
OPCODE_CLOSE
,
OPCODE_PING
,
OPCODE_PONG
...
...
@@ -65,10 +64,10 @@ class WebSocket(object):
payload
=
''
.
join
([
f
.
payload
for
f
in
frames
])
return
create_message
(
frames
[
0
].
opcode
,
payload
)
def
handshake
(
self
):
def
server_
handshake
(
self
):
"""
Execute a handshake
with the oth
er end point of the socket. If the HTTP
request headers
read from the socke
t are invalid, an InvalidRequest
Execute a handshake
as the serv
er end point of the socket. If the HTTP
request headers
sent by the clien
t are invalid, an InvalidRequest
exception is raised.
"""
raw_headers
=
self
.
sock
.
recv
(
512
).
decode
(
'utf-8'
,
'ignore'
)
...
...
@@ -141,15 +140,6 @@ class WebSocket(object):
except
Exception
as
e
:
self
.
onexception
(
e
)
def
run_threaded
(
self
,
daemon
=
True
):
"""
Spawn a new thread that receives messages in an endless loop.
"""
thread
=
Thread
(
target
=
self
.
receive_forever
)
thread
.
daemon
=
daemon
thread
.
start
()
return
thread
def
send_close
(
self
,
code
,
reason
):
"""
Send a close control frame.
...
...
@@ -193,7 +183,7 @@ class WebSocket(object):
self
.
sock
.
close
()
if
frame
.
opcode
!=
OPCODE_CLOSE
:
raise
ValueError
(
'
E
xpected close frame, got %s instead'
%
frame
)
raise
ValueError
(
'
e
xpected close frame, got %s instead'
%
frame
)
def
onopen
(
self
):
"""
...
...
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