Ilya Gorchakov
Ilya Gorchakov

Reputation: 41

Phantomjs if proxy not respond

Script test.js:

var page = require('webpage').create();
var url = args[1];
page.open(url, function (status) {
    console.log(status); 
    phantom.exit();
});

Run script:

phantomjs --proxy=1.1.1.1:22 test.js 'http://nonexistent_site.com'

1.1.1.1:22 - nonexistent server

http://nonexistent_site.com - nonexistent site

How can I determine in PhantomJS which one is not responding - a proxy or a site?

Upvotes: 2

Views: 638

Answers (1)

Vaviloff
Vaviloff

Reputation: 16838

You can catch network timeouts with page.onResourceTimeout callback:

page.onResourceTimeout = function(request) {
    console.log('Response (#' + request.id + '): ' + JSON.stringify(request));
};

You can also set your own timeout:

page.settings.resourceTimeout = 3000; // ms

To intercept network errors you can register page.onResourceError callback:

page.onResourceError = function(resourceError) {
  console.log('Unable to load resource #' + resourceError.id + ' URL:' + resourceError.url);
  console.log('Error code: ' + resourceError.errorCode + '. Description: ' + resourceError.errorString);
};

With this in place, non-existent host will trigger Host not found error.

But if you use a non-working proxy, you will always end up with error Network timeout on resource first, even if target host does not exist.

So if you want to check proxies :) I'd suggest just to page.open hosts that are 100% working, for example, set up a simple static web page on the very server that you are operating from.

Also there is a node.js module: proxy-checker

Upvotes: 2

Related Questions