Created
March 11, 2017 03:39
-
-
Save zweite/f93e9f024b99bd93ed7afb525e692559 to your computer and use it in GitHub Desktop.
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
"use strict"; | |
var url = require('url'); | |
var port, server, service, | |
system = require('system'); | |
var webpage = require('webpage'); | |
function main() { | |
if (system.args.length !== 2) { | |
console.log('Usage: serverkeepalive.js <portnumber>'); | |
phantom.exit(1); | |
} else { | |
port = system.args[1]; | |
server = require('webserver').create(); | |
service = server.listen(port, { keepAlive: true }, function (request, response) { | |
var url_parts = url.parse(request.url, true); | |
var query = url_parts.query; | |
if (query.hasOwnProperty('url')) { | |
var query_url = decodeURIComponent(query['url']) | |
console.log(query_url) | |
browser_query(query_url) | |
} | |
var body = JSON.stringify(request, null, 4); | |
response.statusCode = 200; | |
response.headers = { | |
'Cache': 'no-cache', | |
'Content-Type': 'text/plain', | |
'Connection': 'Keep-Alive', | |
'Keep-Alive': 'timeout=5, max=100', | |
'Content-Length': body.length | |
}; | |
response.write(body); | |
response.close(); | |
}); | |
if (service) { | |
console.log('Web server running on port ' + port); | |
} else { | |
console.log('Error: Could not create web server listening on port ' + port); | |
phantom.exit(); | |
} | |
} | |
} | |
function browser_query(address) { | |
var page = webpage.create() | |
// page.settings.userAgent = "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/56.0.2924.87 Safari/537.36" | |
// page.viewportSize = { width: 1920, height: 1080 } | |
page.onResourceRequested = function (req) { | |
console.log('requested: ' + JSON.stringify(req, undefined, 4)); | |
}; | |
page.onResourceReceived = function (res) { | |
console.log('received: ' + JSON.stringify(res, undefined, 4)); | |
}; | |
page.onLoadFinished = function() { | |
// | |
} | |
t = Date.now(); | |
page.open(address, function (status) { | |
if (status !== 'success') { | |
console.log('FAIL to load the address'); | |
} else { | |
setTimeout(function(){ | |
page.render("123.png") | |
page.close() | |
}, 5000) | |
} | |
}); | |
} | |
// run main | |
main() | |
// test | |
// phantomjs web_brower_service.js 8899 | |
// curl -s 'http://localhost:8899/?url=http%3A%2F%2Fwww.tianyancha.com%2Fcompany%2F270929355' |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment