Search code examples
pythondebuggingpycharmproxychains

Running Python Script in PyCharm Debugger with `proxychains`


I am working with PyCharm IDE and have encountered an issue where I need to run my Python script within both the PyCharm internal debugger and under proxychains. The reason for using proxychains is that the modules I'm working with do not support working via a SOCKS5 proxy, which is necessary for internet access.

Running the script directly from the internal debugger does not provide an option to use proxychains, and using the built-in console does not allow me to step through the code with the debugger.

Is there a way to configure PyCharm to run my script with both the internal debugger and proxychains? I want to be able to debug my code while also routing network requests through proxychains. Any help or suggestions on how to achieve this would be greatly appreciated.

IDE version: PyCharm 2021.3 (Community Edition) under Linux


Solution

  • This can be accomplished by creating a shell script which would run your script through ProxyChains and then using this script as an interpreter in PyCharm.

    First, create a shell script named python in your project directory (let's say it's located at ~/MyProject):

    #!/bin/sh
    proxychains4 /usr/bin/python3 "$@"
    

    $@ is used to pass all the arguments (specifically, path to your main script) through this script to the actual Python interpreter (/usr/bin/python3).

    Make this script executable:

    chmod +x ~/MyProject/python
    

    Now we can add this script as an interpreter in PyCharm.

    If you're not using a virtual environment for this project, you can just add a new system interpreter and pick the created python shell script (~/MyProject/python) as the executable. Then add a new configuration using this new interpreter and path to your main script.

    If you need a virtual environment for this, create a new virtual environment, specify its location and path to your default python interpreter (say, /usr/bin/python3), install all the necessary dependencies inside it and then once again go to Preferences -> Python Interpreter -> Show all, select your newly created virtual environment and change the "interpreter path" field to the shell script path (~/MyProject/python).

    And one more step. Since PyCharm's debugger uses a built-in local server, you need to exclude connection to it in proxychains.conf:

    localnet 127.0.0.1/255.255.255.255
    

    Voilà!

    [proxychains] config file found: /usr/local/etc/proxychains.conf
    [proxychains] preloading /usr/local/Cellar/proxychains-ng/4.17/lib/libproxychains4.dylib
    [proxychains] DLL init: proxychains-ng 4.17
    [proxychains] DLL init: proxychains-ng 4.17
    Connected to pydev debugger (build 233.13763.11)
    [proxychains] Strict chain  ...  171.244.140.160:3991  ... api.ipify.org:443  ...  OK
    {'ip': '171.244.140.160'}
    

    enter image description here