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
| import json | |
| class RPCProxy: | |
| def __init__(self, connection): | |
| self._connection = connection | |
| def __getattr__(self, name): | |
| def do_rpc(*args, **kwargs): | |
| self._connection.send(json.dumps((name, args, kwargs))) | |
| result = json.loads(self._connection.recv()) | |
| return result |
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
| # rpcserver.py | |
| import json | |
| class RPCHandler: | |
| def __init__(self): | |
| self._functions = { } | |
| def register_function(self, func): | |
| self._functions[func.__name__] = func |
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
| import pickle | |
| class RPCProxy: | |
| def __init__(self, connection): | |
| self._connection = connection | |
| def __getattr__(self, name): | |
| def do_rpc(*args, **kwargs): | |
| self._connection.send(pickle.dumps((name, args, kwargs))) | |
| result = pickle.loads(self._connection.recv()) | |
| if isinstance(result, Exception): |
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
| # rpcserver.py | |
| import pickle | |
| class RPCHandler: | |
| def __init__(self): | |
| self._functions = { } | |
| def register_function(self, func): | |
| self._functions[func.__name__] = func |
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
| # A basic GET request | |
| from urllib import request, parse | |
| # Base URL being accessed | |
| url = 'http://httpbin.org/get' | |
| # Dictionary of query parameters (if any) | |
| parms = { | |
| 'name1' : 'value1', |
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
| # A basic POST request | |
| from urllib import request, parse | |
| # Base URL being accessed | |
| url = 'http://httpbin.org/post' | |
| # Dictionary of query parameters (if any) | |
| parms = { | |
| 'name1' : 'value1', |
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
| # A POST request using requests library | |
| import requests | |
| # Base URL being accessed | |
| url = 'http://httpbin.org/post' | |
| # Dictionary of query parameters (if any) | |
| parms = { | |
| 'name1' : 'value1', | |
| 'name2' : 'value2' |
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 a HEAD request | |
| import requests | |
| resp = requests.head('http://www.python.org/index.html') | |
| status = resp.status_code | |
| last_modified = resp.headers['last-modified'] | |
| content_type = resp.headers['content-type'] | |
| content_length = resp.headers['content-length'] |
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 | |
| s = socket(AF_INET, SOCK_STREAM) | |
| s.connect(('localhost', 15000)) | |
| s.send(b'Hello\n') | |
| print('Got:', s.recv(8192)) | |
| s.send(b'World\n') | |
| print('Got:', s.recv(8192)) | |
| s.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
| # server.py | |
| import socket | |
| import struct | |
| def send_fd(sock, fd): | |
| ''' | |
| Send a single file descriptor. | |
| ''' | |
| sock.sendmsg([b'x'], | |
| [(socket.SOL_SOCKET, socket.SCM_RIGHTS, struct.pack('i', fd))]) |