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()
yes, I did zrok enable token and I got the shared URL but it is not loading, keep scrolling, its been 2 days but no solution. I am sharing all the zrok code I am using on kaggle notebook:
!pip install zrok==0.4.47
!pip install waitress
!mkdir -p /kaggle/working/zrok
%cd /kaggle/working/zrok
!wget https://github.com/openziti/zrok/releases/download/v0.4.47/zrok_0.4.47_linux_amd64.tar.gz
!tar -xvf ./zrok*.gz
!chmod a+x /kaggle/working/zrok/zrok
#set the endpoint to work
!/kaggle/working/zrok/zrok config set apiEndpoint https://api.zrok.io
# Enable zrok with your token
token = "my_secret_token"
!/kaggle/working/zrok/zrok enable $token
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()
it is giving me share URL but nothing is loading and no error so far. but if I use: !kaggle/working/zrok/zrok share public [http://localhost](http://localhost/):{port} --headless
then I got the URL and it works but not the one you suggested. please help.
I signed up for Kaggle to see how it works. Is your goal to proxy to a local webserver like http://127.0.0.1:8188 that is provided by a previous cell in the same notebook, a different notebook, or some other background service?
I'm very new at Kaggle, and I had trouble running the first commands in your example, e.g. pip install waitress. I tried the default simple index and pypi index but I keep getting a timeout. It's as if my Kaggle instance can't reach the internet to install from PyPi. After some forum diving, I suspect internet-enabled kernels are disabled for brand-new Kaggle users.
EDIT: I finally found a "session options" toggle for the internet access in the sidebar. It's disabled with tooltip: "Your account must be phone verified to access the internet."
After I learned how to enable internet access for my Kaggle kernel, I was able to use zrok's ProxyShare by following your example and making some modifications while I was troubleshooting and learning to use Kaggle.