Search code examples
pythonlinuxproxytor

Is there a way to enable tor as system proxy settings through Python in Linux


I am working on a project which requires accessing some websites with and without tor enabled, and record the content differences. I am working on deepin (which is a linux based debian distro), and using Python 2.7 to accomplish the task. The problem is that I have to manually enable/disable tor, and change the system proxy settings every time I run the script. Now, I am aware that I can issue a shell command from Python itself to enable tor (service tor start), but I cannot figure out how to enable/disable the system proxy settings from Python.

I have already tried this, but no luck.


Solution

  • Got the thing working, posting here in case someone else has the same problem:

    from selenium import webdriver
    import stem.process
    from stem import Signal
    from stem.control import Controller
    from splinter import Browser
    proxyIP = "127.0.0.1"
    proxyPort = 9050
    proxy_settings = {"network.proxy.type":0,
        "network.proxy.socks": proxyIP,
        "network.proxy.socks_port": proxyPort
    }
    browser = Browser('firefox', profile_preferences=proxy_settings)
    driver = browser.driver
    driver.get('https://whatismyip.com')
    

    changing network.proxy.type to 1 resets the proxy settings. Solution found here