python sdk work

This commit is contained in:
Cam
2023-10-17 11:24:43 -05:00
parent 2d6cd3a6ae
commit 4d6f79f696
111 changed files with 12051 additions and 22 deletions

View File

@ -0,0 +1,36 @@
# zrok Pastebin
This example shows the use of the zrok SDK spinning up a simple pastebin command.
## Setup :wrench:
Refer to the [setup guide](../../../docs/guides/self-hosting/self_hosting_guide.md) for details on setting up your zrok environment needed for this example.
### Install Python Requirements
If you haven't already installed them, you'll need the dependent libraries used in the examples.
```bash
pip install -r ../requirements
```
## 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:
```shell
echo "this is a cool test" | python pastebin.py copyto
```
You should see some helpful info printed out to your terminal:
```shell
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:
```shell
python pastebin.py pastefrom vp0xgmknvisu
```
we see the data we had piped into the `copyto` server:
```
this is a cool test
```

100
sdk/python/examples/pastebin.py Executable file
View File

@ -0,0 +1,100 @@
#!python3
import argparse
import sys
import os
import zrok
from zrok.model import AccessRequest, ShareRequest
from http.server import BaseHTTPRequestHandler, HTTPServer
import urllib3
class MyServer(BaseHTTPRequestHandler):
def __init__(self, data, *args, **kwargs):
self.data = data
super(MyServer, self).__init__(*args, **kwargs)
def do_GET(self):
self.send_response(200)
self.send_header("Content-type", "text/plain")
self.send_header("Content-length", len(self.data))
self.end_headers()
self.wfile.write(bytes(self.data, "utf-8"))
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)
data = self.loadData()
def handler(*args):
MyServer(data, *args)
zrok.monkeypatch(bindHost="127.0.0.1", bindPort=8082, root=root, shrToken=shr.Token)
webServer = HTTPServer(("127.0.0.1", 8082), handler)
print("access your pastebin using 'pastebin.py pastefrom " + shr.Token + "'")
try:
webServer.serve_forever(poll_interval=600)
except KeyboardInterrupt:
pass
webServer.server_close()
zrok.share.DeleteShare(root, shr)
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)
zrok.monkeypatch(bindHost="127.0.0.1", bindPort=8082, root=root, shrToken=options.shrToken)
http = urllib3.PoolManager()
try:
r = http.request('GET', "http://" + options.shrToken)
except Exception as e:
print("Error on request: ", e)
zrok.access.DeleteAccess(root, acc)
return
print(r.data.decode('utf-8'))
try:
zrok.access.DeleteAccess(root, acc)
except Exception as e:
print("unable to delete access", e)
sys.exit(1)
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()
options.func(options)

View File

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