I 'm writing python program that opens a link in Google Chrome/macOS but I need to iterate through proxy list from proxylist.txt file while opening the link.
Is there any way to force specific proxy when opening the link in the browser? and when using the subprocess module?
Here is my code:
import os
import subprocess as sp
import time
def browse(url, dur):
browser = "open -a 'Google Chrome'"
child = sp.Popen(browser+" %s" % url, shell=True)
time.sleep(int(dur))
child.terminate()
os.system("killall -9 'Google Chrome'")
url_link = input("Enter link: ")
duration = input("Enter duration in seconds: ")
browse(url_link, duration)
Below is the updated code following the comments received:
import subprocess as sp
import time
def browse(url, dur, proxy_host, proxy_port):
browser = ['google-chrome', url,
'--proxy-server={host}:{port}'.format(host=proxy_host, port=proxy_port)]
child = sp.Popen(browser)
time.sleep(int(dur))
child.terminate()
url_link = input("Enter link: ")
duration = input("Enter duration in seconds: ")
with open("proxylist.txt", "r") as file:
for line in file:
line = line.rstrip()
myProxy = line.split(':')[0]
myPort = line.split(':')[1]
counter = 1
while (counter <= 10):
print("Count: " + str(counter) + ", opening link " + url_link + " -- proxy id: " + myProxy + ":" + myPort)
browse(url_link, duration, myProxy, myPort)
counter += 1
However i received following error:
File "/xxxxx/subprocess.py", line 1344, in _execute_child
raise child_exception_type(errno_num, err_msg, err_filename)
FileNotFoundError: [Errno 2] No such file or directory: 'google-chrome': 'google-chrome'
open -a 'Google Chrome'instead of thegoogle-chromecommand? The latter has an option to specify a proxy server:google-chrome %s --proxy-server=your.server.com:8000