diff --git a/.gitignore b/.gitignore index d7e839e..7b3cc71 100644 --- a/.gitignore +++ b/.gitignore @@ -131,3 +131,5 @@ dmypy.json .idea/ tags +*.jwt +*.json diff --git a/sample/flask-of-ziti/helloFlazk.py b/sample/flask-of-ziti/helloFlazk.py index e6d3aea..3b82d27 100644 --- a/sample/flask-of-ziti/helloFlazk.py +++ b/sample/flask-of-ziti/helloFlazk.py @@ -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] diff --git a/sample/ziti-echo-server/ziti-echo-client.py b/sample/ziti-echo-server/ziti-echo-client.py new file mode 100644 index 0000000..2df6771 --- /dev/null +++ b/sample/ziti-echo-server/ziti-echo-client.py @@ -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])) \ No newline at end of file diff --git a/sample/ziti-echo-server/ziti-echo-server.py b/sample/ziti-echo-server/ziti-echo-server.py index 137032f..a3b1b71 100644 --- a/sample/ziti-echo-server/ziti-echo-server.py +++ b/sample/ziti-echo-server/ziti-echo-server.py @@ -13,8 +13,13 @@ # 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) @@ -22,18 +27,27 @@ def run(ziti_id, 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]) \ No newline at end of file + 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() \ No newline at end of file