Connecting to a remote IPython instance

后端 未结 5 2202
野的像风
野的像风 2020-11-29 17:38

I would like to run an IPython instance on one machine and connect to it (over LAN) from a different process (to run some python commands). I understand that it is possible

5条回答
  •  Happy的楠姐
    2020-11-29 18:09

    If you just want to connect interactively, you can use SSH forwarding. I didn't find this documented anywhere on Stack Overflow yet, yet this question comes closest. This answer has been tested on Ipython 0.13. I got the information from this blog post.

    1. Run ipython kernel on the remote machine:

      user@remote:~$ ipython3 kernel
      [IPKernelApp] To connect another client to this kernel, use:
      [IPKernelApp] --existing kernel-25333.json
      
    2. Look at the kernel-25333.json file:

      user@remote:~$ cat ~/.ipython/profile_default/security/kernel-25333.json 
      {
        "stdin_port": 54985, 
        "ip": "127.0.0.1", 
        "hb_port": 50266, 
        "key": "da9c7ae2-02aa-47d4-8e67-e6153eb15366", 
        "shell_port": 50378, 
        "iopub_port": 49981
      }
      
    3. Set up port-forwarding on the local machine:

      user@local:~$ ssh user@remote -f -N -L 54985:127.0.0.1:54985
      user@local:~$ ssh user@remote -f -N -L 50266:127.0.0.1:50266
      user@local:~$ ssh user@remote -f -N -L 50378:127.0.0.1:50378
      user@local:~$ ssh user@remote -f -N -L 49981:127.0.0.1:49981
      
    4. Copy the kernel-25333.json file to the local machine:

      user@local:~$ rsync -av user@remote:.ipython/profile_default/security/kernel-25333.json ~/.ipython/profile_default/security/kernel-25333.json
      
    5. Run ipython on the local machine using the new kernel:

      user@local:~$ ipython3 console --existing kernel-25333.json
      Python 3.2.3 (default, Oct 19 2012, 19:53:16)
      Type "copyright", "credits" or "license" for more information.
      
      IPython 0.13.1.rc2 -- An enhanced Interactive Python.
      ?         -> Introduction and overview of IPython's features.
      %quickref -> Quick reference.
      help      -> Python's own help system.
      object?   -> Details about 'object', use 'object??' for extra details.
      
      
      In [1]: import socket; print(socket.gethostname())
      remote
      

提交回复
热议问题