M
M
Maks Pain2019-08-14 10:59:52
JavaScript
Maks Pain, 2019-08-14 10:59:52

How to automatically change the proxy address?

Hey! Tell me pliz how to implement automatic change of proxy address on http request?
There is a list of proxy addresses in a text file and one address is randomly taken from it into a variable.
The bottom line is that now, if during the request there are any errors that are displayed in the console in the server response, or a non-working proxy comes across, the requests continue to go and the error may repeat. How to make it so that in such a situation a new proxy address is immediately substituted so that requests work normally?
Code below:

var proxyList = fs.readFileSync('proxy-list.txt').toString().split("\n");
var randomIp = Math.floor(Math.random() * proxyList.length);
var currentIp = proxyList[randomIp]

setInterval(function() {

  Request.get({
      url: 'https://www.example.com/',
      json: true,
      headers: {'User-Agent': 'request'},
      proxy: 'http://' + currentIp // случайный proxy из массива
    }, (err, res, body) => {
      if (err) {
        console.log('Error:', err);
      } else if (res.statusCode !== 200) {
        console.log('Status:', res.statusCode);
      } else {
  	    console.log(body);
      }
  });

}, 2000);

Answer the question

In order to leave comments, you need to log in

1 answer(s)
R
RokeAlvo, 2019-08-14
@RokeAlvo

You need all the code, so it's not clear what you want to do.
Will remove the "bad" proxy from the array
newProxies = proxyList.filter(i => currentIp != i)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question