-
-
Save UlisesGascon/179bc380bde5a6a2f361f29a9dc85f6b to your computer and use it in GitHub Desktop.
Zero-dependency Python 3 and Node IPC using UNIX sockets
This file contains hidden or 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 python3 | |
# -*- coding: UTF-8 -*- | |
import socket | |
import json | |
server_address = '/tmp/example.sock' | |
sock = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM) | |
sock.connect(server_address) | |
msg = json.dumps({"ping": "hello"}).encode('UTF-8') | |
sock.send(msg) | |
sock.send(b"\r\n") | |
data = sock.recv(256) | |
print(data.decode('UTF-8')) | |
sock.close() |
This file contains hidden or 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
var net = require('net'); | |
var server = net.createServer(client => { | |
const chunks = []; | |
console.log(`client connected`); | |
client.setEncoding('utf8'); | |
client.on('end', () => { | |
console.log('client disconnected'); | |
}); | |
client.on('data', chunk => { | |
console.log(`Got data: ${chunk}`); | |
chunks.push(chunk) | |
if (chunk.match(/\r\n$/)) { | |
const {ping} = JSON.parse(chunks.join('')); | |
client.write(JSON.stringify({pong: ping})); | |
} | |
}); | |
}); | |
server.on('listening', () => { | |
console.log(`Server listening`); | |
}); | |
server.listen('/tmp/example.sock'); | |
/*eslint no-console: false*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment