Search code examples
pythoniframesshproxyjupyter-lab

Open a website via JupyterLab using remote machine (similar to proxy)


I connect via SSH to a remote machine that is running JupyterLab. When I run the following code in JupyterLab I see the IP address of my local machine:

from IPython.display import IFrame

IFrame(src="https://whatismyipaddress.com/", width='100%', height='500px')

Is there a way to access the website using the remote machine's connection? Ideally, the solution will allow me to interact with the website via the IFrame, and the snippet above would display IP of the remote machine.


Solution

  • The IFrame method generates a HTML widget with an <iframe>, which loads the website using your local browser's connection. If you use python code instead, the connection is made from your remote machine, on which the jupyter kernel is running. The following code should display the IP address of your remote machine:

    import requests
    print(requests.get('https://ipinfo.io').json())
    

    If you really need to access the website using your remote machine's connection, you could set up an SSH dynamic forward (i.e. use ssh -D localhost:8080 remote_machine) and then instruct your browser to connect to the internet using the generated SOCKS5 proxy on localhost port 8080. Most browsers support this natively, but there are also handy add-ons such as Proxy SwitchyOmega, which let you define custom rules for each host. There should be plenty of tutorials online how to set this up. If you have a stable connection to your remote machine, this usually works very well and is reasonably fast.