Skip to content

Commit 417f2f2

Browse files
authored
Merge pull request Pithikos#1 from Pithikos/master
renew
2 parents 0fc5156 + 17b6f38 commit 417f2f2

File tree

8 files changed

+368
-315
lines changed

8 files changed

+368
-315
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
*.pyc
2+
.cache

README.md

Lines changed: 26 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -7,27 +7,33 @@ A minimal Websockets Server in Python with no external dependencies.
77
* Clean simple API
88
* Multiple clients
99
* No dependencies
10-
10+
1111
Notice that this implementation does not support the more advanced features
1212
like SSL etc. The project is focused mainly on making it easy to run a
1313
websocket server for prototyping, testing or for making a GUI for your application.
1414

15+
If this project reduced your development time feel free to buy me a coffee.
16+
17+
[![Donate](https://www.paypal.com/en_US/i/btn/x-click-but21.gif)](https://www.paypal.me/seferidis)
18+
1519

1620
Usage
1721
=======================
1822
You can get a feel of how to use the websocket server by running
1923

2024
python server.py
21-
25+
2226
Then just open `client.html` in your browser and you should be able to send and receive messages.
2327

2428

2529
Using in your project
2630
=======================
27-
You can either simply copy/paste the *websocket_server.py* file in your project and use it directly (recommended)
28-
or you can install the project directly from PyPi (might not be up-to-date):
2931

30-
pip install websocket-server
32+
You can use the project in three ways.
33+
34+
1. Copy/paste the *websocket_server.py* file in your project and use it directly
35+
2. `pip install git+https://github.com/Pithikos/python-websocket-server` (latest code)
36+
3. `pip install websocket-server` (might not be up-to-date)
3137

3238
For coding details have a look at the [*server.py*](https://github.com/Pithikos/python-websocket-server/blob/master/server.py) example and the [API](https://github.com/Pithikos/python-websocket-server#api).
3339

@@ -39,18 +45,23 @@ The API is simply methods and properties of the `WebsocketServer` class.
3945

4046
## WebsocketServer
4147

42-
The WebsocketServer takes two arguments: a `port` and a `hostname`.
43-
By default the localhost `127.0.0.1` is used. However if you want to be able and connect
44-
to the server from the network you need to pass `0.0.0.0` as hostname e.g. `WebsocketServer(13254, host='0.0.0.0')`.
48+
The WebsocketServer can be initialized with the below parameters.
4549

46-
###Properties
50+
*`port`* - The port clients will need to connect to.
51+
52+
*`host`* - By default the `127.0.0.1` is used which allows connections only from the current machine. If you wish to allow all network machines to connect, you need to pass `0.0.0.0` as hostname.
53+
54+
*`loglevel`* - logging level to print. By default WARNING is used. You can use `logging.DEBUG` or `logging.INFO` for more verbose output.
55+
56+
57+
### Properties
4758

4859
| Property | Description |
4960
|----------|----------------------|
5061
| clients | A list of `client` |
5162

5263

53-
###Methods
64+
### Methods
5465

5566
| Method | Description | Takes | Gives |
5667
|-----------------------------|---------------------------------------------------------------------------------------|-----------------|-------|
@@ -61,7 +72,7 @@ to the server from the network you need to pass `0.0.0.0` as hostname e.g. `Webs
6172
| `send_message_to_all()` | Sends a `message` to **all** connected clients. The message is a simple string. | message | None |
6273

6374

64-
###Callback functions
75+
### Callback functions
6576

6677
| Set by | Description | Parameters |
6778
|-----------------------------|---------------------------------------------------|-------------------------|
@@ -70,23 +81,23 @@ to the server from the network you need to pass `0.0.0.0` as hostname e.g. `Webs
7081
| `set_fn_message_received()` | Called when a `client` sends a `message` | client, server, message |
7182

7283

73-
The client passed to the callback is the client that left, sent the message, etc. The server might not have any use to use. However it is
74-
passed in case you want to send messages to clients.
84+
The client passed to the callback is the client that left, sent the message, etc. The server might not have any use to use. However it is passed in case you want to send messages to clients.
7585

7686

7787
Example:
7888
````
89+
import logging
7990
from websocket_server import WebsocketServer
8091
8192
def new_client(client, server):
8293
server.send_message_to_all("Hey all, a new client has joined us")
8394
84-
server = WebsocketServer(13254, host='127.0.0.1')
95+
server = WebsocketServer(13254, host='127.0.0.1', loglevel=logging.INFO)
8596
server.set_fn_new_client(new_client)
8697
server.run_forever()
8798
````
8899

89-
##Client
100+
## Client
90101

91102
Client is just a dictionary passed along methods.
92103

@@ -97,4 +108,3 @@ Client is just a dictionary passed along methods.
97108
'address' : (addr, port)
98109
}
99110
````
100-

tests/README.md

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
Testing
2+
--------
3+
4+
Run unit tests
5+
6+
pytest
7+
8+
9+
Run functional tests
10+
11+
python message_lengths.py
12+
13+
Open client.html in the browser and refresh consequently until all test cases pass.

tests/_bootstrap_.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
#Bootstrap
22
import sys, os
3-
if 'python-websockets-server' in os.getcwd():
3+
if os.getcwd().endswith('tests'):
44
sys.path.insert(0, '..')
5+
elif os.getcwd().endswith('websocket-server'):
6+
sys.path.insert(0, '.')

tests/handshake.py

Lines changed: 0 additions & 40 deletions
This file was deleted.

tests/message_lengths.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,20 @@
11
import _bootstrap_
2-
from websocket import WebSocketsServer
2+
from websocket_server import WebsocketServer
33
from time import sleep
44
from testsuite.messages import *
55

66
'''
7-
This creates just a server that will send a different message to every new connection:
8-
7+
This creates just a server that will send a different message to every new connection:
8+
99
1. A message of length less than 126
1010
2. A message of length 126
1111
3. A message of length 127
1212
4. A message of length bigger than 127
1313
5. A message above 1024
1414
6. A message above 65K
1515
7. An enormous message (well beyond 65K)
16-
17-
16+
17+
1818
Reconnect to get the next message
1919
'''
2020

@@ -52,6 +52,6 @@ def new_client(client, server):
5252

5353

5454
PORT=9001
55-
server = WebSocketsServer(PORT)
55+
server = WebsocketServer(PORT)
5656
server.set_fn_new_client(new_client)
5757
server.run_forever()

tests/test_handshake.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import _bootstrap_
2+
from websocket_server import *
3+
import pytest
4+
5+
6+
class DummyWebsocketHandler(WebSocketHandler):
7+
def __init__(self, *_):
8+
pass
9+
10+
@pytest.fixture
11+
def websocket_handler():
12+
return DummyWebsocketHandler()
13+
14+
def test_hash_calculations_for_response(websocket_handler):
15+
key = 'zyjFH2rQwrTtNFk5lwEMQg=='
16+
expected_key = '2hnZADGmT/V1/w1GJYBtttUKASY='
17+
assert websocket_handler.calculate_response_key(key) == expected_key
18+
19+
20+
def test_response_messages(websocket_handler):
21+
key = 'zyjFH2rQwrTtNFk5lwEMQg=='
22+
expected = \
23+
'HTTP/1.1 101 Switching Protocols\r\n'\
24+
'Upgrade: websocket\r\n' \
25+
'Connection: Upgrade\r\n' \
26+
'Sec-WebSocket-Accept: 2hnZADGmT/V1/w1GJYBtttUKASY=\r\n'\
27+
'\r\n'
28+
handshake_content = websocket_handler.make_handshake_response(key)
29+
assert handshake_content == expected

0 commit comments

Comments
 (0)