Created
December 15, 2011 21:38
-
-
Save rafalw/1483025 to your computer and use it in GitHub Desktop.
Modyfikacja kodu z gist 1390021
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env node | |
// Bardzo^3 prosty CURL-like | |
// Wywołanie: | |
// node requesturl.js adres_strony_bez_http | |
// np.: | |
// node requesturl.js wileczeknet.blogspot.com | |
// Wersja 2 | |
// - zastąpiono klasyczną obsługę parametrów linii polecenia funkcjonalnością dostarczaną | |
// przez moduł "optimist" (opis: http://www.catonmat.net/blog/nodejs-modules-optimist/) | |
// TODO: | |
// - w miarę możliwości rozbudować program (dodać inne opcje, np. wysyłanie danych formularza itp.) | |
var http = require("http"), | |
argv = require("optimist").argv; | |
if (argv._.length == 1) { | |
// Prawdopodobnie podano adres strony, można rozpocząć pobieranie | |
console.log("Rozpoczynam pobieranie strony '" + argv._[0] + "'"); | |
// Port 80 hardcoded ;-) | |
// A przy okazji: można użyć http.get zamiast http.request (jesli już hardcodujemy)... | |
var req = http.request({ host: argv._[0], port: 80, path: "/", method: "GET" }, function(response) { | |
var data = ""; | |
var chunks = 0; | |
response.setEncoding('utf-8'); | |
response.on("data", function(chunk) { | |
data += chunk; | |
chunks++; | |
}); | |
response.on("end", function() { | |
console.log(data); | |
console.log("\nLiczba paczek: " + chunks); | |
console.log("Liczba odebranych znaków: " + data.length); | |
console.log("Liczba odebranych bajtów: " + Buffer.byteLength(data, 'utf-8')); | |
}); | |
}); | |
req.on("error", function(err) { | |
console.log("Błąd: " + err.message); | |
}); | |
req.end(); | |
} else { | |
console.log("Błędna liczba parametrów"); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment