Skip to content

Instantly share code, notes, and snippets.

@seunggabi
Last active January 27, 2020 12:43
Show Gist options
  • Save seunggabi/2fb31b7ec287bc6e6253cc547db80149 to your computer and use it in GitHub Desktop.
Save seunggabi/2fb31b7ec287bc6e6253cc547db80149 to your computer and use it in GitHub Desktop.
proxy.py
# reference: https://wkdtjsgur100.github.io/selenium-change-ip/
# brew install geckodriver
# brew install tor
# brew services restart tor
from selenium import webdriver
from bs4 import BeautifulSoup
import os
class Proxy:
def __init__(self):
os.system("brew services restart tor")
profile = webdriver.FirefoxProfile()
profile.set_preference("network.proxy.type", 1)
# netstat -anp tcp | grep -i "LISTEN"
# tor browser start (port 9050)
profile.set_preference("network.proxy.socks", "127.0.0.1")
profile.set_preference("network.proxy.socks_port", 9050)
profile.set_preference("browser.cache.disk.enable", False)
profile.set_preference("browser.cache.memory.enable", False)
profile.set_preference("browser.cache.offline.enable", False)
profile.set_preference("network.http.use-cache", False)
profile.set_preference("network.cookie.cookieBehavior", 2)
profile.set_preference("browser.privatebrowsing.autostart", True)
profile.update_preferences()
driver = webdriver.Firefox(profile)
driver.delete_all_cookies()
self.driver = driver
def ip(self, close=True):
d = self.driver
d.get("http://icanhazip.com/")
o = BeautifulSoup(d.page_source, "html.parser")
print(o.findAll("pre")[0].text)
if close:
d.quit()
return self
def open(self, url, close=False):
d = self.driver
d.get(url)
if close:
d.quit()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment