test_ip.py 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. import traceback
  2. from selenium import webdriver
  3. from selenium.webdriver.common.by import By
  4. from selenium.webdriver.common.keys import Keys
  5. from selenium.webdriver.chrome.service import Service
  6. import json
  7. import redis
  8. import random
  9. import os
  10. import time
  11. import redis
  12. driver = None
  13. def re_get_webdriver():
  14. global port
  15. global driver
  16. result = []
  17. if driver is not None:
  18. print('closing....')
  19. driver.quit()
  20. os.system('killall chrome')
  21. print('quit....')
  22. driver = None
  23. try:
  24. s = Service('/root/driver/chromedriver102')
  25. #s = Service('/root/driver/chromedriver')
  26. options = webdriver.ChromeOptions()
  27. options.add_argument("--no-sandbox")
  28. options.add_argument("--disable-dev-shm-usage")
  29. options.add_argument("--headless")
  30. #options.add_argument('--remote-debugging-port=9222')
  31. #options.add_experimental_option("debuggerAddress", '127.0.0.1:9927')
  32. options.add_argument("--incognito")
  33. r = redis.Redis(host='db.ptt.cx', port=6379, db=2, password='choozmo9')
  34. data = r.get('google_proxy')
  35. jstext = data.decode('utf-8')
  36. jsobj = json.loads(jstext)
  37. proxy = random.choice(jsobj)
  38. change_ip_list = ['--proxy-server=%s' % proxy, "--proxy-server=socks5://127.0.0.1:9050",
  39. "--proxy-server=socks5://192.53.174.202:8180"]
  40. change_ip = random.choice(change_ip_list)
  41. #options.add_argument('--proxy-server=socks5://172.104.93.163:41800')
  42. print('使用代理ip', change_ip)
  43. #driver.delete_all_cookies()
  44. driver = webdriver.Chrome(options=options)
  45. driver.set_window_size(1400, 1000)
  46. except:
  47. traceback.print_exc()
  48. driver = None
  49. return None
  50. def run_once():
  51. global driver
  52. re_get_webdriver()
  53. driver.get('https://api.ipify.org/')
  54. time.sleep(3)
  55. print(driver.current_url)
  56. ip_address = driver.find_element(By.TAG_NAME, "body").text
  57. print(ip_address)
  58. driver.quit()
  59. #os.system('docker container restart tiny6')
  60. time.sleep(1)
  61. run_once()