Funktionierender Prototyp des Serious Games zur Vermittlung von Wissen zu Software-Engineering-Arbeitsmodellen.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

pullpipe.py 1.2KB

1 year ago
12345678910111213141516171819202122232425262728293031323334353637383940
  1. # -*- test-case-name: twisted.python.test.test_sendmsg -*-
  2. # Copyright (c) Twisted Matrix Laboratories.
  3. # See LICENSE for details.
  4. import os
  5. import socket
  6. import sys
  7. from struct import unpack
  8. from typing import Tuple
  9. from twisted.python.sendmsg import recvmsg
  10. def recvfd(socketfd: int) -> Tuple[int, bytes]:
  11. """
  12. Receive a file descriptor from a L{sendmsg} message on the given C{AF_UNIX}
  13. socket.
  14. @param socketfd: An C{AF_UNIX} socket, attached to another process waiting
  15. to send sockets via the ancillary data mechanism in L{send1msg}.
  16. @param fd: C{int}
  17. @return: a 2-tuple of (new file descriptor, description).
  18. @rtype: 2-tuple of (C{int}, C{bytes})
  19. """
  20. ourSocket = socket.fromfd(socketfd, socket.AF_UNIX, socket.SOCK_STREAM)
  21. data, ancillary, flags = recvmsg(ourSocket)
  22. [(cmsgLevel, cmsgType, packedFD)] = ancillary
  23. # cmsgLevel and cmsgType really need to be SOL_SOCKET / SCM_RIGHTS, but
  24. # since those are the *only* standard values, there's not much point in
  25. # checking.
  26. [unpackedFD] = unpack("i", packedFD)
  27. return (unpackedFD, data)
  28. if __name__ == "__main__":
  29. fd, description = recvfd(int(sys.argv[1]))
  30. os.write(fd, b"Test fixture data: " + description + b".\n")
  31. os.close(fd)