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
0cb48c88
Commit
0cb48c88
authored
Aug 31, 2013
by
Taddeüs Kroes
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Changed package name to 'wspy'
parent
02204dd3
Changes
7
Hide whitespace changes
Inline
Side-by-side
Showing
7 changed files
with
41 additions
and
41 deletions
+41
-41
README.md
README.md
+19
-19
connection.py
connection.py
+5
-5
python_digest.py
python_digest.py
+1
-1
server.py
server.py
+3
-3
setup.py
setup.py
+5
-5
test/client.html
test/client.html
+1
-1
websocket.py
websocket.py
+7
-7
No files found.
README.md
View file @
0cb48c88
About
About
=====
=====
*
t
wspy*
is a standalone implementation of web sockets for Python, defined by
*wspy*
is a standalone implementation of web sockets for Python, defined by
[
RFC 6455
](
http://tools.ietf.org/html/rfc6455
)
. The incentive for creating this
[
RFC 6455
](
http://tools.ietf.org/html/rfc6455
)
. The incentive for creating this
library is the absence of a layered implementation of web sockets outside the
library is the absence of a layered implementation of web sockets outside the
scope of web servers such as Apache or Nginx.
*
t
wspy*
does not require any
scope of web servers such as Apache or Nginx.
*wspy*
does not require any
third-party programs or libraries outside Python's standard library. It
third-party programs or libraries outside Python's standard library. It
provides low-level access to sockets, as well as high-level functionalities to
provides low-level access to sockets, as well as high-level functionalities to
easily set up a web server. Thus, it is both suited for quick server
easily set up a web server. Thus, it is both suited for quick server
...
@@ -29,8 +29,8 @@ Installation
...
@@ -29,8 +29,8 @@ Installation
Use Python's package manager:
Use Python's package manager:
easy_install
t
wspy
easy_install wspy
pip install
t
wspy
pip install wspy
Basic usage
Basic usage
...
@@ -43,22 +43,22 @@ Basic usage
...
@@ -43,22 +43,22 @@ Basic usage
Server example:
Server example:
import
t
wspy, socket
import wspy, socket
sock =
t
wspy.websocket()
sock = wspy.websocket()
sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
sock.bind(('', 8000))
sock.bind(('', 8000))
sock.listen(5)
sock.listen(5)
client = sock.accept()
client = sock.accept()
client.send(
twspy.Frame(t
wspy.OPCODE_TEXT, 'Hello, Client!'))
client.send(
wspy.Frame(
wspy.OPCODE_TEXT, 'Hello, Client!'))
frame = client.recv()
frame = client.recv()
Client example:
Client example:
import
t
wspy
import wspy
sock =
t
wspy.websocket(location='/my/path')
sock = wspy.websocket(location='/my/path')
sock.connect(('', 8000))
sock.connect(('', 8000))
sock.send(
twspy.Frame(t
wspy.OPCODE_TEXT, 'Hello, Server!'))
sock.send(
wspy.Frame(
wspy.OPCODE_TEXT, 'Hello, Server!'))
-
A
`Connection`
instance represents a connection between two end points, based
-
A
`Connection`
instance represents a connection between two end points, based
on a
`websocket`
instance. A connection handles control frames properly, and
on a
`websocket`
instance. A connection handles control frames properly, and
...
@@ -69,20 +69,20 @@ Basic usage
...
@@ -69,20 +69,20 @@ Basic usage
Example of an echo server (sends back what it receives):
Example of an echo server (sends back what it receives):
import
t
wspy
import wspy
class EchoConnection(
t
wspy.Connection):
class EchoConnection(wspy.Connection):
def onopen(self):
def onopen(self):
print 'Connection opened at %s:%d' % self.sock.getpeername()
print 'Connection opened at %s:%d' % self.sock.getpeername()
def onmessage(self, message):
def onmessage(self, message):
print 'Received message "%s"' % message.payload
print 'Received message "%s"' % message.payload
self.send(
t
wspy.TextMessage(message.payload))
self.send(wspy.TextMessage(message.payload))
def onclose(self, message):
def onclose(self, message):
print 'Connection closed'
print 'Connection closed'
server =
t
wspy.websocket()
server = wspy.websocket()
server.bind(('', 8000))
server.bind(('', 8000))
server.listen(5)
server.listen(5)
...
@@ -99,10 +99,10 @@ Basic usage
...
@@ -99,10 +99,10 @@ Basic usage
**Note:**
For browser clients, you will probably want to use JSON encoding.
**Note:**
For browser clients, you will probably want to use JSON encoding.
This could, for example, be implemented as follows:
This could, for example, be implemented as follows:
import
t
wspy, json
import wspy, json
def msg(**data):
def msg(**data):
return
t
wspy.TextMessage(json.dumps(data))
return wspy.TextMessage(json.dumps(data))
# create some connection `conn`...
# create some connection `conn`...
...
@@ -120,15 +120,15 @@ Basic usage
...
@@ -120,15 +120,15 @@ Basic usage
For example, the
`EchoConnection`
example above can be rewritten to:
For example, the
`EchoConnection`
example above can be rewritten to:
import
t
wspy
import wspy
class EchoServer(
t
wspy.Server):
class EchoServer(wspy.Server):
def onopen(self, client):
def onopen(self, client):
print 'Client %s connected' % client
print 'Client %s connected' % client
def onmessage(self, client, message):
def onmessage(self, client, message):
print 'Received message "%s"' % message.payload
print 'Received message "%s"' % message.payload
client.send(
t
wspy.TextMessage(message.payload))
client.send(wspy.TextMessage(message.payload))
def onclose(self, client):
def onclose(self, client):
print 'Client %s disconnected' % client
print 'Client %s disconnected' % client
...
...
connection.py
View file @
0cb48c88
...
@@ -17,20 +17,20 @@ class Connection(object):
...
@@ -17,20 +17,20 @@ class Connection(object):
class should implement the on*() event handlers.
class should implement the on*() event handlers.
Example of an echo server (sends back what it receives):
Example of an echo server (sends back what it receives):
>>> import
t
wspy
>>> import wspy
>>> class EchoConnection(
t
wspy.Connection):
>>> class EchoConnection(wspy.Connection):
>>> def onopen(self):
>>> def onopen(self):
>>> print 'Connection opened at %s:%d' % self.sock.getpeername()
>>> print 'Connection opened at %s:%d' % self.sock.getpeername()
>>> def onmessage(self, message):
>>> def onmessage(self, message):
>>> print 'Received message "%s"' % message.payload
>>> print 'Received message "%s"' % message.payload
>>> self.send(
t
wspy.TextMessage(message.payload))
>>> self.send(wspy.TextMessage(message.payload))
>>> def onclose(self, message):
>>> def onclose(self, message):
>>> print 'Connection closed'
>>> print 'Connection closed'
>>> server =
t
wspy.websocket()
>>> server = wspy.websocket()
>>> server.bind(('', 8000))
>>> server.bind(('', 8000))
>>> server.listen()
>>> server.listen()
...
@@ -214,7 +214,7 @@ class Connection(object):
...
@@ -214,7 +214,7 @@ class Connection(object):
For example, to add an automatic JSON conversion to messages and
For example, to add an automatic JSON conversion to messages and
eliminate the need to contruct TextMessage instances to all messages:
eliminate the need to contruct TextMessage instances to all messages:
>>> import
t
wspy, json
>>> import wspy, json
>>> conn = Connection(...)
>>> conn = Connection(...)
>>> conn.add_hook(lambda data: tswpy.TextMessage(json.dumps(data)),
>>> conn.add_hook(lambda data: tswpy.TextMessage(json.dumps(data)),
>>> lambda message: json.loads(message.payload))
>>> lambda message: json.loads(message.payload))
...
...
python_digest.py
View file @
0cb48c88
...
@@ -26,7 +26,7 @@ OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
...
@@ -26,7 +26,7 @@ OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
This code was downloaded from https://github.com/dimagi/python-digest to avoid
This code was downloaded from https://github.com/dimagi/python-digest to avoid
having to download python-digest as a dependency for
t
wspy.
having to download python-digest as a dependency for wspy.
'''
'''
try
:
try
:
...
...
server.py
View file @
0cb48c88
...
@@ -15,15 +15,15 @@ class Server(object):
...
@@ -15,15 +15,15 @@ class Server(object):
Websocket server, manages multiple client connections.
Websocket server, manages multiple client connections.
Example usage:
Example usage:
>>> import
t
wspy
>>> import wspy
>>> class EchoServer(
t
wspy.Server):
>>> class EchoServer(wspy.Server):
>>> def onopen(self, client):
>>> def onopen(self, client):
>>> print 'Client %s connected' % client
>>> print 'Client %s connected' % client
>>> def onmessage(self, client, message):
>>> def onmessage(self, client, message):
>>> print 'Received message "%s"' % message.payload
>>> print 'Received message "%s"' % message.payload
>>> client.send(
t
wspy.TextMessage(message.payload))
>>> client.send(wspy.TextMessage(message.payload))
>>> def onclose(self, client):
>>> def onclose(self, client):
>>> print 'Client %s disconnected' % client
>>> print 'Client %s disconnected' % client
...
...
setup.py
View file @
0cb48c88
...
@@ -2,12 +2,12 @@
...
@@ -2,12 +2,12 @@
from
distutils.core
import
setup
from
distutils.core
import
setup
setup
(
name
=
'
t
wspy'
,
setup
(
name
=
'wspy'
,
version
=
'0.
8
'
,
version
=
'0.
9
'
,
description
=
'A standalone implementation of websockets (RFC 6455).'
,
description
=
'A standalone implementation of websockets (RFC 6455).'
,
author
=
'Taddeus Kroes'
,
author
=
'Taddeus Kroes'
,
author_email
=
'taddeuskroes@gmail.com'
,
author_email
=
'taddeuskroes@gmail.com'
,
url
=
'https://github.com/taddeus/
t
wspy'
,
url
=
'https://github.com/taddeus/wspy'
,
package_dir
=
{
'
t
wspy'
:
'.'
},
package_dir
=
{
'wspy'
:
'.'
},
packages
=
[
'
t
wspy'
],
packages
=
[
'wspy'
],
license
=
'3-clause BSD License'
)
license
=
'3-clause BSD License'
)
test/client.html
View file @
0cb48c88
<!doctype html>
<!doctype html>
<html>
<html>
<head>
<head>
<title>
t
wspy echo test client
</title>
<title>
wspy echo test client
</title>
</head>
</head>
<body>
<body>
<textarea
id=
"log"
rows=
"20"
cols=
"80"
readonly=
"readonly"
></textarea>
<textarea
id=
"log"
rows=
"20"
cols=
"80"
readonly=
"readonly"
></textarea>
...
...
websocket.py
View file @
0cb48c88
...
@@ -20,21 +20,21 @@ class websocket(object):
...
@@ -20,21 +20,21 @@ class websocket(object):
illustrated by the examples below.
illustrated by the examples below.
Server example:
Server example:
>>> import
t
wspy, socket
>>> import wspy, socket
>>> sock =
t
wspy.websocket()
>>> sock = wspy.websocket()
>>> sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
>>> sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
>>> sock.bind(('', 8000))
>>> sock.bind(('', 8000))
>>> sock.listen(5)
>>> sock.listen(5)
>>> client = sock.accept()
>>> client = sock.accept()
>>> client.send(
twspy.Frame(t
wspy.OPCODE_TEXT, 'Hello, Client!'))
>>> client.send(
wspy.Frame(
wspy.OPCODE_TEXT, 'Hello, Client!'))
>>> frame = client.recv()
>>> frame = client.recv()
Client example:
Client example:
>>> import
t
wspy
>>> import wspy
>>> sock =
t
wspy.websocket(location='/my/path')
>>> sock = wspy.websocket(location='/my/path')
>>> sock.connect(('', 8000))
>>> sock.connect(('', 8000))
>>> sock.send(
twspy.Frame(t
wspy.OPCODE_TEXT, 'Hello, Server!'))
>>> sock.send(
wspy.Frame(
wspy.OPCODE_TEXT, 'Hello, Server!'))
"""
"""
def
__init__
(
self
,
sock
=
None
,
protocols
=
[],
extensions
=
[],
origin
=
None
,
def
__init__
(
self
,
sock
=
None
,
protocols
=
[],
extensions
=
[],
origin
=
None
,
location
=
'/'
,
trusted_origins
=
[],
locations
=
[],
auth
=
None
,
location
=
'/'
,
trusted_origins
=
[],
locations
=
[],
auth
=
None
,
...
@@ -179,7 +179,7 @@ class websocket(object):
...
@@ -179,7 +179,7 @@ class websocket(object):
For example, the following code creates a `Frame` instance for data
For example, the following code creates a `Frame` instance for data
being sent and removes the instance for received data. This way, data
being sent and removes the instance for received data. This way, data
can be sent and received as if on a regular socket.
can be sent and received as if on a regular socket.
>>> import
t
wspy
>>> import wspy
>>> sock.add_hook(lambda data: tswpy.Frame(tswpy.OPCODE_TEXT, data),
>>> sock.add_hook(lambda data: tswpy.Frame(tswpy.OPCODE_TEXT, data),
>>> lambda frame: frame.payload)
>>> lambda frame: frame.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