zrok/sdk/python/examples/http-server/server.py
Cam Otts 3e6ab2b39b
Python SDK Update (#523)
* added first iteration decorator for zrok and example flask server

* update requirements. Add context managing for share and access. Updated pastebin example to better cleanup

* setup and sample tweaks

* small linting updates and example changes

* A few small fixes

* fix long description

* Update the ignore file.
Considering moving location of this.

* Added flake8 linting for builds

* use python 3.10

* move setup python to its own block

* added back in the py name

* update changelogs and add readme

---------

Signed-off-by: Cam Otts <otts.cameron@gmail.com>
Co-authored-by: Kenneth Bingham <kenneth.bingham@netfoundry.io>
2024-01-17 10:46:19 -06:00

49 lines
1.3 KiB
Python
Executable File

#!python3
from flask import Flask
import sys
import zrok
from zrok.model import ShareRequest
import atexit
app = Flask(__name__)
zrok_opts = {}
bindPort = 18081
@zrok.decor.zrok(opts=zrok_opts)
def runApp():
from waitress import serve
# the port is only used to integrate Zrok with frameworks that expect a "hostname:port" combo
serve(app, port=bindPort)
@app.route('/')
def hello_world():
print("received a request to /")
return "Look! It's zrok!"
if __name__ == '__main__':
root = zrok.environment.root.Load()
try:
shr = zrok.share.CreateShare(root=root, request=ShareRequest(
BackendMode=zrok.model.TCP_TUNNEL_BACKEND_MODE,
ShareMode=zrok.model.PUBLIC_SHARE_MODE,
Frontends=['public'],
Target="http-server"
))
shrToken = shr.Token
print("Access server at the following endpoints: ", "\n".join(shr.FrontendEndpoints))
def removeShare():
zrok.share.DeleteShare(root=root, shr=shr)
print("Deleted share")
atexit.register(removeShare)
except Exception as e:
print("unable to create share", e)
sys.exit(1)
zrok_opts['cfg'] = zrok.decor.Opts(root=root, shrToken=shrToken, bindPort=bindPort)
runApp()