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
| # Example of file descriptor passing using multiprocessing | |
| import multiprocessing | |
| from multiprocessing.reduction import recv_handle, send_handle | |
| import socket | |
| def worker(in_p, out_p): | |
| out_p.close() | |
| while True: | |
| fd = recv_handle(in_p) |
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
| # servermp.py | |
| from multiprocessing.connection import Listener | |
| from multiprocessing.reduction import send_handle | |
| import socket | |
| def server(work_address, port): | |
| # Wait for the worker to connect | |
| work_serv = Listener(work_address, authkey=b'peekaboo') | |
| worker = work_serv.accept() | |
| worker_pid = worker.recv() |
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
| # worker.py | |
| import socket | |
| import struct | |
| def recv_fd(sock): | |
| ''' | |
| Receive a single file descriptor | |
| ''' | |
| msg, ancdata, flags, addr = sock.recvmsg(1, | |
| socket.CMSG_LEN(struct.calcsize('i'))) |
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
| # workermp.py | |
| from multiprocessing.connection import Client | |
| from multiprocessing.reduction import recv_handle | |
| import os | |
| import socket | |
| def worker(server_address): | |
| serv = Client(server_address, authkey=b'peekaboo') | |
| serv.send(os.getpid()) |
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
| # auth.py | |
| import hmac | |
| import os | |
| def client_authenticate(connection, secret_key): | |
| ''' | |
| Authenticate client to a remote service. | |
| connection represents a network connection. | |
| secret_key is a key known only to both client/server. |
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
| from socket import socket, AF_INET, SOCK_STREAM | |
| from auth import client_authenticate | |
| secret_key = b'peekaboo' | |
| s = socket(AF_INET, SOCK_STREAM) | |
| s.connect(('localhost', 18000)) | |
| client_authenticate(s, secret_key) | |
| s.send(b'Hello World') | |
| resp = s.recv(1024) |
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
| from socket import socket, AF_INET, SOCK_STREAM | |
| from auth import server_authenticate | |
| secret_key = b'peekaboo' | |
| def echo_handler(client_sock): | |
| if not server_authenticate(client_sock, secret_key): | |
| client_sock.close() | |
| return | |
| while True: |
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
| from multiprocessing.connection import Client | |
| c = Client(('localhost', 25000), authkey=b'peekaboo') | |
| c.send('hello') | |
| print('Got:', c.recv()) | |
| c.send(42) | |
| print('Got:', c.recv()) | |
| c.send([1, 2, 3, 4, 5]) | |
| print('Got:', c.recv()) | |
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
| from multiprocessing.connection import Listener | |
| import traceback | |
| def echo_client(conn): | |
| try: | |
| while True: | |
| msg = conn.recv() | |
| conn.send(msg) | |
| except EOFError: | |
| print('Connection closed') |
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
| from xmlrpc.client import ServerProxy | |
| s = ServerProxy('http://localhost:15000', allow_none=True) | |
| s.set('foo', 'bar') | |
| s.set('spam', [1, 2, 3]) | |
| print(s.keys()) | |
| print(s.get('foo')) | |
| print(s.get('spam')) | |
| s.delete('spam') | |
| print(s.exists('spam')) |