scripts: websocket: make ssl optional
Tested:
The following command worked.
```
python scripts/websocket_test.py --host localhost:18080 --no-ssl
```
Signed-off-by: Nan Zhou <nanzhoumails@gmail.com>
Change-Id: Ib14bd8ecdb5f0cad4a8e0478d69abcdc3e46c3ec
diff --git a/scripts/websocket_test.py b/scripts/websocket_test.py
index c76a014..85b2c0d 100755
--- a/scripts/websocket_test.py
+++ b/scripts/websocket_test.py
@@ -15,6 +15,8 @@
parser.add_argument(
"--username", help="Username to connect with", default="root")
parser.add_argument("--password", help="Password to use", default="0penBmc")
+parser.add_argument('--ssl', default=True,
+ action=argparse.BooleanOptionalAction)
args = parser.parse_args()
@@ -32,13 +34,17 @@
async def hello():
- uri = 'wss://{}/subscribe'.format(args.host)
+ protocol = 'ws'
+ if args.ssl:
+ protocol += 's'
+ uri = '{}://{}/subscribe'.format(protocol, args.host)
ssl_context = ssl.SSLContext()
authbytes = "{}:{}".format(args.username, args.password).encode('ascii')
auth = "Basic {}".format(base64.b64encode(authbytes).decode('ascii'))
headers = {"Authorization": auth}
async with \
websockets.connect(uri, ssl=ssl_context, extra_headers=headers) \
+ if args.ssl else websockets.connect(uri, extra_headers=headers) \
as websocket:
request = json.dumps({
"paths": ["/xyz/openbmc_project/sensors"],
@@ -59,4 +65,5 @@
continue
print(f"{name:<20} {value:4.02f} {units}")
+
asyncio.get_event_loop().run_until_complete(hello())