0

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'
3
  • 1
    You could perhaps add the --proxy-server="proxy" to your command when opening the browser. Commented Jul 27, 2018 at 17:50
  • 1
    any particular reason you need to use open -a 'Google Chrome' instead of the google-chrome command? The latter has an option to specify a proxy server: google-chrome %s --proxy-server=your.server.com:8000 Commented Jul 27, 2018 at 17:50
  • I receive following error when using 'google-chrome' --> FileNotFoundError: [Errno 2] No such file or directory: 'google-chrome': 'google-chrome' Commented Jul 27, 2018 at 19:45

1 Answer 1

2

Don't use shell=True. Instead try executing google chrome directly without executing the shell. No need to call a shell process to execute a program when you can call the program you want directly:

import os
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: ")


browse(url_link, duration, 'my_proxy', 1234)

By not using the shell you can also use .terminate() to terminate the subprocess - no need to run killall because the process you're running is actually the one you want to terminate, not the shell.

Sign up to request clarification or add additional context in comments.

2 Comments

I receive following error when using 'google-chrome' --> FileNotFoundError: [Errno 2] No such file or directory: 'google-chrome': 'google-chrome'
It doesn't find the runnable browser when I'm not using shell=True so it gives the error no such file or directory...please advise how to solve this issue with the above updated code? It works with webbrowser.get('chrome' or 'firefox').open(url), can I pass proxy-server option with this command? one more point, how can I confirm that the link is really opened with the provided proxy? @user:17160

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.