Skip to content

Commit 3d7325e

Browse files
Add option for host address in echo server and MQTT broker (#113)
* Update echo server to use host address * Update MQTT broker to use host address * Fix CI checks * Fix CI checks
1 parent d9151c9 commit 3d7325e

File tree

4 files changed

+33
-5
lines changed

4 files changed

+33
-5
lines changed

localhost-echo-server/action.yml

+10-1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@ name: 'Localhost Echo Server'
22
description: 'Starts an echo server using Python.'
33

44
inputs:
5+
host_address:
6+
description: "Host address for echo server."
7+
required: false
8+
default: "127.0.0.1"
59
port_number:
610
description: "Port for echo server."
711
required: True
@@ -13,5 +17,10 @@ runs:
1317
shell: bash
1418
run: |
1519
python3 --version
16-
python3 $GITHUB_ACTION_PATH/local_echo_server.py --port_number=${{ inputs.port_number }} &
20+
# Use the host_address input if provided, otherwise default to 127.0.0.1
21+
HOST=${{ inputs.host_address }}
22+
if [[ -z "$HOST" ]]; then
23+
HOST="127.0.0.1"
24+
fi
25+
python3 $GITHUB_ACTION_PATH/local_echo_server.py --host=$HOST --port_number=${{ inputs.port_number }} &
1726

localhost-echo-server/local_echo_server.py

+5-1
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,10 @@ async def echo_handler(reader, writer):
1717

1818
if __name__ == '__main__':
1919
parser = ArgumentParser(description='Localhost Echo server.')
20+
parser.add_argument('--host',
21+
type=str,
22+
required=True,
23+
help='Host address for the echo server.')
2024
parser.add_argument('--port_number',
2125
type=int,
2226
required=True,
@@ -28,7 +32,7 @@ async def echo_handler(reader, writer):
2832
asyncio.set_event_loop(loop)
2933
factory = asyncio.start_server(
3034
echo_handler,
31-
os.environ.get('HOST'),
35+
os.environ.get('HOST', args.host),
3236
os.environ.get('PORT', args.port_number)
3337
)
3438
server = loop.run_until_complete(factory)

localhost-mqtt-broker/action.yml

+12-1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@ name: 'Localhost MQTT Broker'
22
description: 'Starts an MQTT Broker using Python. For TLS connections (including mutual authentication), connect to localhost:8883. For plaintext connections, connect to localhost:1883.'
33

44
inputs:
5+
host_address:
6+
description: "Host address for echo server."
7+
required: false
8+
default: "127.0.0.1"
59
root-ca-cert-path:
610
description: "Root CA certificate file path."
711
required: True
@@ -19,5 +23,12 @@ runs:
1923
run: pip install -r $GITHUB_ACTION_PATH/requirements.txt
2024
shell: bash
2125
- name: Run localhost MQTT broker
22-
run: python3 $GITHUB_ACTION_PATH/localhost_mqtt_broker.py --root-ca-cert-path=${{ inputs.root-ca-cert-path }} --server-priv-key-path=${{ inputs.server-priv-key-path }} --server-cert-path=${{ inputs.server-cert-path }} &
26+
run: |
27+
python3 --version
28+
# Use the host_address input if provided, otherwise default to 127.0.0.1
29+
HOST=${{ inputs.host_address }}
30+
if [[ -z "$HOST" ]]; then
31+
HOST="127.0.0.1"
32+
fi
33+
python3 $GITHUB_ACTION_PATH/localhost_mqtt_broker.py --host=$HOST --root-ca-cert-path=${{ inputs.root-ca-cert-path }} --server-priv-key-path=${{ inputs.server-priv-key-path }} --server-cert-path=${{ inputs.server-cert-path }} &
2334
shell: bash

localhost-mqtt-broker/localhost_mqtt_broker.py

+6-2
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,10 @@
1212
# Parse passed in credentials
1313
parser = ArgumentParser(description='Localhost MQTT broker.')
1414

15+
parser.add_argument('--host',
16+
type=str,
17+
required=True,
18+
help='Host address for the echo server.')
1519
parser.add_argument('--root-ca-cert-path',
1620
type=str,
1721
required=True,
@@ -31,12 +35,12 @@
3135
"listeners": {
3236
"default": {
3337
"type": "tcp",
34-
"bind": f"{LOCAL_HOST_IP}:1883",
38+
"bind": f"{args.host}:1883",
3539
"max-connections": 1000,
3640
},
3741
"tls": {
3842
"type": "tcp",
39-
"bind": f"{LOCAL_HOST_IP}:8883",
43+
"bind": f"{args.host}:8883",
4044
"max-connections": 1000,
4145
"ssl": "on",
4246
"cafile": args.root_ca_cert_path,

0 commit comments

Comments
 (0)