Skip to content

Commit

Permalink
allow server to respond to ctrl-c even when blocked in the socket 'ac…
Browse files Browse the repository at this point in the history
…cept' call
  • Loading branch information
dovholuknf committed Oct 26, 2023
1 parent 35ec070 commit d4307c5
Showing 1 changed file with 15 additions and 1 deletion.
16 changes: 15 additions & 1 deletion sample/ziti-echo-server/ziti-echo-server.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,27 +13,41 @@
# limitations under the License.

import sys
import signal
import threading
import openziti

def signal_handler(signum, frame):
print("\nCtrl-C detected. Exiting...")
sys.exit(0)

def run(ziti_id, service):
ztx = openziti.load(ziti_id)
server = ztx.bind(service)
server.listen()

while True:
print('beginning accept')
conn, peer = server.accept()
print(f'processing incoming client[{peer}]')
with conn:
count = 0
while True:
print('starting receive')
data = conn.recv(1024)
if not data:
print(f'client finished after sending {count} bytes')
break
count += len(data)
conn.sendall(data)
print(f'received data from client. returning {count} bytes of data now...')


if __name__ == '__main__':
run(sys.argv[1], sys.argv[2])
signal.signal(signal.SIGINT, signal_handler)
# Create a separate thread to run the server so we can respond to ctrl-c when in 'accept'
server_thread = threading.Thread(target=run, args=(sys.argv[1], sys.argv[2]))
server_thread.start()

# Wait for the server thread to complete or Ctrl-C to be detected
server_thread.join()

0 comments on commit d4307c5

Please sign in to comment.