-
-
Notifications
You must be signed in to change notification settings - Fork 529
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix #354.
Showing
2 changed files
with
25 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
#!/usr/bin/env python | ||
|
||
import asyncio | ||
import http | ||
import websockets | ||
|
||
class ServerProtocol(websockets.WebSocketServerProtocol): | ||
|
||
async def process_request(self, path, request_headers): | ||
if path == '/__health__/': | ||
return http.HTTPStatus.OK, [], b'OK\n' | ||
|
||
async def echo(websocket, path): | ||
async for message in websocket: | ||
await websocket.send(message) | ||
|
||
start_server = websockets.serve( | ||
echo, 'localhost', 8765, create_protocol=ServerProtocol) | ||
|
||
asyncio.get_event_loop().run_until_complete(start_server) | ||
asyncio.get_event_loop().run_forever() |
849666b
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would add to that
elif path not in ['/', '/socket']:
return BAD_REQUEST, [('Content-Length', 0)], ""
otherwise just going to the root of the domain will cause exceptions that will propagate to Pager Duty !
Bots that visit your domain will cause lots of errors.
Your example might as well built in some best practices. It is good to make a recommendation to name your url end point '/socket' so that you can filter out the socket requests explicitly from random connection to root of the domain by bots.
849666b
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, there's room for further improvements to solve #369 — perhaps improvements to the code and not just examples in the docs.