HP.
HP.

Reputation: 19896

Casper --proxy doesn't work

Here is my code to check for IP address using http://whatismyipaddress.com/proxy-check

var casper = require("casper").create(),
    utils = require('utils');

casper.userAgent('Mozilla/5.0 (Windows NT 6.1; WOW64; rv:17.0) Gecko/20100101 Firefox/17.0');

casper.start("http://whatismyipaddress.com/proxy-check", function() {
  this.capture('0.png');
});

casper.on('remote.message', function(msg) {
    this.echo('Remote: ' + msg);
})

casper.run(function() {
  this.echo('End');

  //finish execution script 
  this.exit();  

});

I typed casperjs --proxy="xxx.xxx.xxx.xxx:80" proxy.js But the screenshot is exactly the IP address from my machine.

So how do I know if the proxy is bad or if something wrong in my code or command line?

Upvotes: 3

Views: 8188

Answers (3)

Hemerson Varela
Hemerson Varela

Reputation: 25772

I ran your code and it works fine for me

Running the script without proxy.

casperjs proxy.js

img1

Running the script with proxy.

casperjs --proxy=208.72.118.16:60099 --proxy-auth=username:password proxy.js

img2

Upvotes: 1

Hemerson Varela
Hemerson Varela

Reputation: 25772

If you want to test if casperjs is successfully using the proxy server, you can use the following script test. It makes a request to whatismyip.com and print the IP address used by casperjs.

//create casper object
var casper = require('casper').create();

casper.start('http://www.whatismyip.com/');

//to avoid 'Access Denied'  <!-- Error #1010 -->
//The owner of this website (www.whatismyip.com) has banned  
//your access based on your browser's signature (42c0a6c6-cl-ua-50). (Ref. 1010)
casper.userAgent('Mozilla/5.0 (Macintosh; Intel Mac OS X)');

//Home Page
casper.then(function() {

    if(this.exists('div#greenip')){

        var element = this.evaluate(function() {
            //remove unnecessari nodes
            return jQuery('div#greenip').text().trim();
        });

        console.log(element);   
    }else{
        console.log("ACCESS_DENIED");   
    }    

});

casper.run(function() {

    //finish execution script 
    this.exit();
});

The output:

casperjs --proxy=XXX.XXX.XXX.XXX:60099 --proxy-auth=username:password proxy.js
XXX.XXX.XXX.XXX

Upvotes: 5

simonemainardi
simonemainardi

Reputation: 515

A shorter casperjs script using http://ip-addr.es/

var casper = require('casper').create();
var url = 'http://ip-addr.es/';

casper.start(url, function() {
    var js = this.evaluate(function() {
        return document; 
    }); 
    this.echo(js.all[0].outerText); 
});
casper.run();

Assuming you have saved the previous script as whatismyip.js, and you have a SOCKSv5 proxy listening on localhost port 8000, then you can check if casperjs is currently using a proxy as:

simone@tramontana:~$ casperjs  whatismyip.js 
93.[xx].[xx].226 # I've hidden a couple of bytes of my real IP ;)

simone@tramontana:~$ casperjs --proxy=127.0.0.1:8000 --proxy-type=socks5 whatismyip.js 
46.23.73.4

Upvotes: 4

Related Questions