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
53d9f210
Commit
53d9f210
authored
Nov 07, 2012
by
Taddeüs Kroes
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Rewrote Server/Client using new websocket API
parent
1d141392
Changes
2
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
21 additions
and
23 deletions
+21
-23
TODO
TODO
+2
-0
server.py
server.py
+19
-23
No files found.
TODO
View file @
53d9f210
- Unit tests
- Unit tests
- Mutual exclusion in Server/Client (multiple threads sending stuff at the same
time will go wrong)
- Extensions
- Extensions
server.py
View file @
53d9f210
import
socket
import
socket
import
logging
import
logging
from
traceback
import
format_exc
from
traceback
import
format_exc
from
threading
import
Thread
,
Lock
from
threading
import
Thread
from
websocket
import
WebS
ocket
from
websocket
import
webs
ocket
from
exceptions
import
InvalidRequest
from
connection
import
Connection
from
frame
import
CLOSE_NORMAL
from
frame
import
CLOSE_NORMAL
from
exceptions
import
InvalidRequest
class
Server
(
object
):
class
Server
(
object
):
def
__init__
(
self
,
port
,
address
=
''
,
log_
level
=
logging
.
INFO
,
protocols
=
[]):
def
__init__
(
self
,
port
,
hostname
=
''
,
log
level
=
logging
.
INFO
,
protocols
=
[]):
logging
.
basicConfig
(
level
=
log
_
level
,
logging
.
basicConfig
(
level
=
loglevel
,
format
=
'%(asctime)s: %(levelname)s: %(message)s'
,
format
=
'%(asctime)s: %(levelname)s: %(message)s'
,
datefmt
=
'%H:%M:%S'
)
datefmt
=
'%H:%M:%S'
)
self
.
sock
=
socket
.
socket
(
socket
.
AF_INET
,
socket
.
SOCK_STREAM
)
self
.
sock
=
websocket
(
)
self
.
sock
.
setsockopt
(
socket
.
SOL_SOCKET
,
socket
.
SO_REUSEADDR
,
1
)
self
.
sock
.
setsockopt
(
socket
.
SOL_SOCKET
,
socket
.
SO_REUSEADDR
,
1
)
logging
.
info
(
'Starting server at %s:%d'
,
address
,
port
)
logging
.
info
(
'Starting server at %s:%d'
,
hostname
,
port
)
self
.
sock
.
bind
((
address
,
port
))
self
.
sock
.
bind
((
hostname
,
port
))
self
.
sock
.
listen
(
5
)
self
.
sock
.
listen
(
5
)
self
.
clients
=
[]
self
.
clients
=
[]
...
@@ -27,9 +28,8 @@ class Server(object):
...
@@ -27,9 +28,8 @@ class Server(object):
while
True
:
while
True
:
try
:
try
:
sock
,
address
=
self
.
sock
.
accept
()
sock
,
address
=
self
.
sock
.
accept
()
client
=
Client
(
self
,
sock
,
address
)
client
.
server_handshake
(
)
client
=
Client
(
self
,
sock
,
address
)
self
.
clients
.
append
(
client
)
self
.
clients
.
append
(
client
)
logging
.
info
(
'Registered client %s'
,
client
)
logging
.
info
(
'Registered client %s'
,
client
)
...
@@ -73,22 +73,18 @@ class Server(object):
...
@@ -73,22 +73,18 @@ class Server(object):
msg
+=
' [%d]'
%
code
msg
+=
' [%d]'
%
code
if
len
(
reason
):
if
len
(
reason
):
msg
+=
'
"%s"'
%
reason
msg
+=
'
: '
+
reason
logging
.
debug
(
msg
)
logging
.
debug
(
msg
)
def
onexception
(
self
,
client
,
e
):
logging
.
error
(
format_exc
(
e
))
class
Client
(
WebSocket
):
class
Client
(
Connection
):
def
__init__
(
self
,
server
,
sock
,
address
):
def
__init__
(
self
,
server
,
sock
):
super
(
Client
,
self
).
__init__
(
sock
)
super
(
Client
,
self
).
__init__
(
sock
)
self
.
server
=
server
self
.
server
=
server
self
.
address
=
address
self
.
send_lock
=
Lock
()
def
send_frame
(
self
,
frame
):
self
.
send_lock
.
acquire
()
WebSocket
.
send_frame
(
self
,
frame
)
self
.
send_lock
.
release
()
def
onopen
(
self
):
def
onopen
(
self
):
self
.
server
.
onopen
(
self
)
self
.
server
.
onopen
(
self
)
...
@@ -106,13 +102,13 @@ class Client(WebSocket):
...
@@ -106,13 +102,13 @@ class Client(WebSocket):
self
.
server
.
remove_client
(
self
,
code
,
reason
)
self
.
server
.
remove_client
(
self
,
code
,
reason
)
def
onexception
(
self
,
e
):
def
onexception
(
self
,
e
):
logging
.
error
(
format_exc
(
e
)
)
self
.
server
.
onexception
(
self
,
e
)
def
__str__
(
self
):
def
__str__
(
self
):
return
'<Client at %s:%d>'
%
self
.
address
return
'<Client at %s:%d>'
%
self
.
sock
.
getpeername
()
if
__name__
==
'__main__'
:
if
__name__
==
'__main__'
:
import
sys
import
sys
port
=
int
(
sys
.
argv
[
1
])
if
len
(
sys
.
argv
)
>
1
else
80
port
=
int
(
sys
.
argv
[
1
])
if
len
(
sys
.
argv
)
>
1
else
80
Server
(
port
,
log
_
level
=
logging
.
DEBUG
).
run
()
Server
(
port
,
loglevel
=
logging
.
DEBUG
).
run
()
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