Created
June 16, 2017 03:54
-
-
Save webstory/bbd395911a93766c3168dae0c2800841 to your computer and use it in GitHub Desktop.
Python and Nodejs with subprocess
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
#-*- coding: utf-8 -*- | |
import subprocess | |
if __name__ == '__main__': | |
ps = subprocess.Popen(['nodejs','ps2.js'], stdin=subprocess.PIPE, stdout=subprocess.PIPE) | |
out = ps.communicate(input='http://www.daum.net'.encode())[0] | |
print(out.decode('utf-8')) |
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' | |
const http = require('http') | |
process.stdin.setEncoding('utf-8') | |
process.stdout.setEncoding('utf-8') | |
process.stdin.on('readable', () => { | |
const content = process.stdin.read() | |
if(!!content) { | |
http.get(content, (res) => { | |
if(res.statusCode >= 200 && res.statusCode < 400) { | |
let body = '' | |
res.on('data', (d) => body += d) | |
res.on('end', () => console.log(body)) | |
} else { | |
console.error(`[${res.statusCode}] ${res.statusMessage}`) | |
} | |
}) | |
} | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
After the execution of ps1.py, I am receiving the following error:
FileNotFoundError Traceback (most recent call last)
in
2
3 if name == 'main':
----> 4 ps = subprocess.Popen(['nodejs','ps2.js'], stdin=subprocess.PIPE, stdout=subprocess.PIPE)
5
6 out = ps.communicate(input='http://www.daum.net'.encode())[0]
/usr/lib/python3.7/subprocess.py in init(self, args, bufsize, executable, stdin, stdout, stderr, preexec_fn, close_fds, shell, cwd, env, universal_newlines, startupinfo, creationflags, restore_signals, start_new_session, pass_fds, encoding, errors, text)
798 c2pread, c2pwrite,
799 errread, errwrite,
--> 800 restore_signals, start_new_session)
801 except:
802 # Cleanup if the child failed starting.
/usr/lib/python3.7/subprocess.py in _execute_child(self, args, executable, preexec_fn, close_fds, pass_fds, cwd, env, startupinfo, creationflags, shell, p2cread, p2cwrite, c2pread, c2pwrite, errread, errwrite, restore_signals, start_new_session)
1549 if errno_num == errno.ENOENT:
1550 err_msg += ': ' + repr(err_filename)
-> 1551 raise child_exception_type(errno_num, err_msg, err_filename)
1552 raise child_exception_type(err_msg)
1553
FileNotFoundError: [Errno 2] No such file or directory: 'nodejs': 'nodejs'