Skip to content

Commit

Permalink
Merge pull request #55 from openziti/refactor-echo-sample
Browse files Browse the repository at this point in the history
Refactor echo sample and tiny update to flazk
  • Loading branch information
dovholuknf authored Oct 26, 2023
2 parents 720cb39 + 28b2a05 commit 19b81f5
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 7 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -131,3 +131,5 @@ dmypy.json
.idea/

tags
*.jwt
*.json
10 changes: 4 additions & 6 deletions sample/flask-of-ziti/helloFlazk.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,20 +19,18 @@
app = Flask(__name__)
bind_opts = {} # populated in main


@openziti.zitify(bindings={
':18080': bind_opts,
})
@openziti.zitify(bindings={':18080': bind_opts,})
def runApp():
from waitress import serve
print("starting server on OpenZiti overlay")
#the port is only used to integrate OpenZiti with frameworks that expect a "hostname:port" combo
serve(app,port=18080)


@app.route('/')
def hello_world(): # put application's code here
print("received a request to /")
return 'Have some Ziti!'


if __name__ == '__main__':
bind_opts['ztx'] = sys.argv[1]
bind_opts['service'] = sys.argv[2]
Expand Down
47 changes: 47 additions & 0 deletions sample/ziti-echo-server/ziti-echo-client.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
# Copyright NetFoundry Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# https://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

import sys
import openziti
import socket

def netcat_client(ziti_id, service, port):
try:
zitiContext = openziti.load(ziti_id)
client = openziti.socket(type = socket.SOCK_STREAM) #socket.socket(socket.AF_INET, socket.SOCK_STREAM)

client.connect((service, port))
print(f"Connected to {service}:{port}")

while True:
user_input = input("Enter a message: ")
client.send(user_input.encode("utf-8") + b'\n')

data = client.recv(1024)
if len(data) == 0:
break

print("Server response:", data.decode("utf-8"))

except ConnectionRefusedError:
print(f"Connection to {service}:{port} refused.")
except KeyboardInterrupt:
print("\nConnection closed.")
except Exception as e:
print(f"Error: {e}")
finally:
client.close()

if __name__ == "__main__":
netcat_client(sys.argv[1], sys.argv[2], int(sys.argv[3]))
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 {len(data)} 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 19b81f5

Please sign in to comment.