The ngrok.connect function is replicating (in code) what the zrok share public command is doing. Unfortunately the zrok SDK does not (currently) have a single function you can call like that (from Python) to instantiate the reverse proxy in code.
To do the equivalent of ngrok.connect for zrok, you would need to call zrok.share.CreateShare and also open a network connection to the local resource. With those 2 network connections created (one to the zrok network, and another to the local resource), you would need to use 2 threads to read from one and write to the other, and then also the inverse (read from the other and write to the first).
There's an example in the Python SDK that shows how CreateShare and zrok.listener works:
At some point in the future we can probably provide a single function call that can instantiate a full reverse proxy in Python code.
There's a new proxy package for the Py SDK coming in the next zrok release. It enables a Jupyter notebook to proxy a public or private share to a local target like this:
# %%
! pip install zrok
# %%
import zrok
from zrok.proxy import ProxyShare
# %%
target_url = "http://127.0.0.1:8000/"
unique_name = "myuniquename" # a name to reuse each run or 'None' for random
share_mode = "public" # "public" or "private"
frontend = "public" # custom domain frontend or "public"
if unique_name.lower() == "none":
unique_name = None
# %%
zrok_env = zrok.environment.root.Load() # Load the environment from ~/.zrok
proxy_share = ProxyShare.create(
root=zrok_env,
target=target_url,
frontends=[frontend],
share_mode=share_mode,
unique_name=unique_name,
verify_ssl=True # Set 'False' to skip SSL verification
)
# %%
if share_mode == "public":
print(f"Access proxy at: {', '.join(proxy_share.endpoints)}")
elif share_mode == "private":
print(f"Run a private access frontend: 'zrok access private {proxy_share.token}'")
proxy_share.run()
from zrok.proxy import ProxyShare
import zrok
# Load the environment
root = zrok.environment.root.Load()
# Create a temporary proxy share (will be cleaned up on exit)
proxy = ProxyShare.create(root=root, target="http://my-target-service")
# print the public URL
print(f"Access proxy at: {proxy.endpoints}")
proxy.run()
The Load() function loads your existing zrok user environment from ~/.zrok (or Windows equivalent) that you enabled on this device: Getting Started | Zrok
The error indicates it was unable to find the config.json file in your environment, so it's possible you need to enable a zrok user environment or perhaps there is a bug requiring a config.json file when it might not exist.
EDIT: I found the issue. The SDK requires a config.json file to exist, but it should not require it, only loading it if it exists. Workaround is to create the file by setting at least one configuration directive.
This will set the same value as the default, so it will not change behavior if you are missing a config.json file inside your enabled zrok user environment dir, e.g., ~/.zrok.
after trying a lot I am not able to run it using the way you mentioned if possible may you write a code that can work in kaggle/colab notebook so share ports like 8188 and sometimes able to share multiple ports. that would be so helpful
Sure thing, glad to help. Please help me understand the problem you encountered.
For example, if you are running a local webserver on 8188/TCP you can do this after you set at least one configuration with zrok config set.
from zrok.proxy import ProxyShare
import zrok
# Load the environment
root = zrok.environment.root.Load()
# Create a temporary proxy share (will be cleaned up on exit)
proxy = ProxyShare.create(root=root, target="http://127.0.0.1:8188")
# print the public URL
print(f"Access proxy at: {proxy.endpoints}")
proxy.run()