From 1faa2c82c3a1d79a7a6b0273433f1a79aa0f674a Mon Sep 17 00:00:00 2001 From: "Hancel.Lin" Date: Tue, 3 Apr 2018 17:29:11 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=20Node.js=20=E7=88=AC?= =?UTF-8?q?=E8=99=AB=20Sample=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .gitignore | 6 ++++++ 01/crawler/main.py | 8 ++++++-- 01/crawler/node/app.js | 15 +++++++++++++++ 01/crawler/node/package.json | 14 ++++++++++++++ 4 files changed, 41 insertions(+), 2 deletions(-) create mode 100644 .gitignore create mode 100644 01/crawler/node/app.js create mode 100644 01/crawler/node/package.json diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..490b385 --- /dev/null +++ b/.gitignore @@ -0,0 +1,6 @@ +node_modules +*.obj +*.out +*.pyc +package-lock.json +.vscode \ No newline at end of file diff --git a/01/crawler/main.py b/01/crawler/main.py index c9e3d77..fd529a3 100644 --- a/01/crawler/main.py +++ b/01/crawler/main.py @@ -8,5 +8,9 @@ from urllib2 import urlopen # Python 2 -rsp = urlopen('http://ip.sxisa.com/') -print(rsp.read()) \ No newline at end of file +def ip(): + rsp = urlopen('http://ip.sxisa.com/') + return rsp.read() + +if __name__ == '__main__': + print(ip()) \ No newline at end of file diff --git a/01/crawler/node/app.js b/01/crawler/node/app.js new file mode 100644 index 0000000..5849a0e --- /dev/null +++ b/01/crawler/node/app.js @@ -0,0 +1,15 @@ +#!/usr/bin/env node +const request = require('request'); + +function ip(callback) { + request('http://ip.sxisa.com/', (err, rsp, body) => { + if (err) throw (err) + if (callback) callback(body) + }) +} + +module.exports = ip; + +if (require.main === module) { + ip(console.log); +} \ No newline at end of file diff --git a/01/crawler/node/package.json b/01/crawler/node/package.json new file mode 100644 index 0000000..48dd2a5 --- /dev/null +++ b/01/crawler/node/package.json @@ -0,0 +1,14 @@ +{ + "name": "crawler", + "version": "1.0.0", + "description": "", + "main": "app.js", + "scripts": { + "start": "node ./app.js" + }, + "author": "", + "license": "ISC", + "dependencies": { + "request": "^2.85.0" + } +}