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>
This commit is contained in:
Cam Otts
2024-01-17 10:46:19 -06:00
committed by GitHub
parent 16e2cff4c9
commit 3e6ab2b39b
30 changed files with 3424 additions and 83 deletions

View File

@ -0,0 +1,55 @@
# zrok Pastebin
This example shows the use of the zrok SDK spinning up a simple pastebin command.
## Self-hosting Setup :wrench:
You don't need this section if you're using hosted zrok from NetFoundry (https://api.zrok.io/).
Refer to the [setup guide](../../../docs/guides/self-hosting/self_hosting_guide.md) for details on setting up your zrok
environment if you're self-hosting zrok.
### Install Python Requirements
The zrok SDK requires Python 3.10 or later.
If you haven't already installed them, you'll need the dependent libraries used in the examples.
```bash
pip install -r ./sdk/python/examples/requirements.txt
```
## Running the Example :arrow_forward:
This example contains a `copyto` server portion and `pastefrom` client portion.
### copyto
The server portion expects to get data you want to send via stdin. It can be evoked by:
```bash
echo "this is a cool test" | python pastebin.py copyto
```
You should see some helpful info printed out to your terminal:
```bash
access your pastebin using 'pastebin.py pastefrom vp0xgmknvisu'
```
The last token in that line is your share token. We'll use that in the pastefrom command to access our data.
### pastefrom
The `pastefrom` client expects the share token as an argument.
If we envoke it using the same token as above:
```bash
python pastebin.py pastefrom vp0xgmknvisu
```
we see the data we had piped into the `copyto` server:
```text
this is a cool test
```

View File

@ -0,0 +1,101 @@
#!python3
import argparse
import atexit
import sys
import os
import zrok
import zrok.listener
import zrok.dialer
from zrok.model import AccessRequest, ShareRequest
import signal
import threading
exit_signal = threading.Event()
def signal_handler(signum, frame):
print("\nCtrl-C detected. Next connection will close server")
exit_signal.set()
class copyto:
def handle(self, *args, **kwargs):
root = zrok.environment.root.Load()
try:
shr = zrok.share.CreateShare(root=root, request=ShareRequest(
BackendMode=zrok.model.TCP_TUNNEL_BACKEND_MODE,
ShareMode=zrok.model.PRIVATE_SHARE_MODE,
Target="pastebin"
))
except Exception as e:
print("unable to create share", e)
sys.exit(1)
def removeShare():
try:
zrok.share.DeleteShare(root, shr)
except Exception as e:
print("unable to delete share", e)
sys.exit(1)
atexit.register(removeShare)
data = self.loadData()
print("access your pastebin using 'pastebin.py pastefrom " + shr.Token + "'")
with zrok.listener.Listener(shr.Token, root) as server:
while not exit_signal.is_set():
conn, peer = server.accept()
with conn:
conn.sendall(data.encode('utf-8'))
print("Server stopped.")
def loadData(self):
if not os.isatty(sys.stdin.fileno()):
return sys.stdin.read()
else:
raise Exception("'copyto' requires input from stdin; direct your paste buffer into stdin")
def pastefrom(options):
root = zrok.environment.root.Load()
try:
acc = zrok.access.CreateAccess(root=root, request=AccessRequest(
ShareToken=options.shrToken,
))
except Exception as e:
print("unable to create access", e)
sys.exit(1)
def removeAccess():
try:
zrok.access.DeleteAccess(root, acc)
except Exception as e:
print("unable to delete access", e)
sys.exit(1)
atexit.register(removeAccess)
client = zrok.dialer.Dialer(options.shrToken, root)
data = client.recv(1024)
print(data.decode('utf-8'))
if __name__ == "__main__":
parser = argparse.ArgumentParser()
subparsers = parser.add_subparsers()
subparsers.required = True
c = copyto()
parser_copyto = subparsers.add_parser('copyto')
parser_copyto.set_defaults(func=c.handle)
parser_pastefrom = subparsers.add_parser('pastefrom')
parser_pastefrom.set_defaults(func=pastefrom)
parser_pastefrom.add_argument("shrToken")
options = parser.parse_args()
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=options.func, args=[options])
server_thread.start()
server_thread.join()

View File

@ -0,0 +1,3 @@
openziti==0.8.1
requests==2.31.0
zrok