| 1 |
# Copyright (c) Twisted Matrix Laboratories. |
| 2 |
# See LICENSE for details. |
| 3 |
|
| 4 |
""" |
| 5 |
Client-side of an example for sending file descriptors between processes over |
| 6 |
UNIX sockets. This client connects to a server listening on a UNIX socket and |
| 7 |
waits for one file descriptor to arrive over the connection. It displays the |
| 8 |
name of the file and the first 80 bytes it contains, then exits. |
| 9 |
|
| 10 |
To run this example, run this program with one argument: a path giving the UNIX |
| 11 |
socket the server side of this example is already listening on. For example: |
| 12 |
|
| 13 |
$ python recvfd.py /tmp/sendfd.sock |
| 14 |
|
| 15 |
See sendfd.py for the server side of this example. |
| 16 |
""" |
| 17 |
|
| 18 |
if __name__ == "__main__": |
| 19 |
import recvfd |
| 20 |
|
| 21 |
raise SystemExit(recvfd.main()) |
| 22 |
|
| 23 |
import os |
| 24 |
import sys |
| 25 |
|
| 26 |
from zope.interface import implementer |
| 27 |
|
| 28 |
from twisted.internet import reactor |
| 29 |
from twisted.internet.defer import Deferred |
| 30 |
from twisted.internet.endpoints import UNIXClientEndpoint |
| 31 |
from twisted.internet.interfaces import IFileDescriptorReceiver |
| 32 |
from twisted.internet.protocol import Factory |
| 33 |
from twisted.protocols.basic import LineOnlyReceiver |
| 34 |
from twisted.python.filepath import FilePath |
| 35 |
from twisted.python.log import startLogging |
| 36 |
|
| 37 |
|
| 38 |
@implementer(IFileDescriptorReceiver) |
| 39 |
class ReceiveFDProtocol(LineOnlyReceiver): |
| 40 |
descriptor = None |
| 41 |
|
| 42 |
def __init__(self): |
| 43 |
self.whenDisconnected = Deferred() |
| 44 |
|
| 45 |
def fileDescriptorReceived(self, descriptor): |
| 46 |
# Record the descriptor sent to us |
| 47 |
self.descriptor = descriptor |
| 48 |
|
| 49 |
def lineReceived(self, line): |
| 50 |
if self.descriptor is None: |
| 51 |
print(f"Received {line} without receiving descriptor!") |
| 52 |
else: |
| 53 |
# Use the previously received descriptor, along with the newly |
| 54 |
# provided information about which file it is, to present some |
| 55 |
# information to the user. |
| 56 |
data = os.read(self.descriptor, 80) |
| 57 |
print(f"Received {line} from the server.") |
| 58 |
print(f"First 80 bytes are:\n{data}\n") |
| 59 |
os.close(self.descriptor) |
| 60 |
self.transport.loseConnection() |
| 61 |
|
| 62 |
def connectionLost(self, reason): |
| 63 |
self.whenDisconnected.callback(None) |
| 64 |
|
| 65 |
|
| 66 |
def main(): |
| 67 |
address = FilePath(sys.argv[1]) |
| 68 |
|
| 69 |
startLogging(sys.stdout) |
| 70 |
|
| 71 |
factory = Factory() |
| 72 |
factory.protocol = ReceiveFDProtocol |
| 73 |
factory.quiet = True |
| 74 |
|
| 75 |
endpoint = UNIXClientEndpoint(reactor, address.path) |
| 76 |
connected = endpoint.connect(factory) |
| 77 |
|
| 78 |
def succeeded(client): |
| 79 |
return client.whenDisconnected |
| 80 |
|
| 81 |
def failed(reason): |
| 82 |
print("Could not connect:", reason.getErrorMessage()) |
| 83 |
|
| 84 |
def disconnected(ignored): |
| 85 |
reactor.stop() |
| 86 |
|
| 87 |
connected.addCallbacks(succeeded, failed) |
| 88 |
connected.addCallback(disconnected) |
| 89 |
|
| 90 |
reactor.run() |