Skip to content

Instantly share code, notes, and snippets.

@soheilpro soheilpro/attack.js
Last active Aug 22, 2017

Embed
What would you like to do?
http test
#!/usr/bin/env node
var http = require('http');
if (process.argv.length <= 2) {
console.log('Usage: node attack.js <ip[:port]> <connections>')
return;
}
var address = process.argv[2];
var host = address.split(':')[0];
var port = address.split(':')[1] || 8080;
var count = process.argv[3];
var options = {
hostname: host,
port: port,
path: '/',
method: 'GET'
};
var results = {};
var r = 0;
function printResults() {
for (var key in results)
console.log(`${results[key]}: ${key}`)
}
for (var i = 0; i < count; i++) {
var req = http.request(options, function(res) {
var key = "OK";
results[key] = (results[key] || 0) + 1;
if (r++ === count - 1)
printResults();
});
req.on('error', function(error) {
var key = error.toString();
results[key] = (results[key] || 0) + 1;
if (r++ === count - 1)
printResults();
});
req.end();
}
#!/usr/bin/env node
var http = require('http');
if (process.argv.length <= 2) {
console.log('Usage: node server.js [port]')
return;
}
var port = process.argv[2] || 8080;
var counter = 0;
var server = http.createServer(function (request, response) {
counter++;
console.log(`Serving ${counter}`);
response.writeHead(200, {"Content-Type": "text/plain"});
response.end("Hello World\n");
});
server.listen(port);
console.log(`Server running at port ${port}`);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.