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.

_socket.py 1.7KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. try:
  2. from socket import inet_pton
  3. except ImportError:
  4. from typing import TYPE_CHECKING
  5. if TYPE_CHECKING: # pragma: no cover
  6. pass
  7. else:
  8. # based on https://gist.github.com/nnemkin/4966028
  9. # this code only applies on Windows Python 2.7
  10. import ctypes
  11. import socket
  12. class SockAddr(ctypes.Structure):
  13. _fields_ = [
  14. ("sa_family", ctypes.c_short),
  15. ("__pad1", ctypes.c_ushort),
  16. ("ipv4_addr", ctypes.c_byte * 4),
  17. ("ipv6_addr", ctypes.c_byte * 16),
  18. ("__pad2", ctypes.c_ulong),
  19. ]
  20. WSAStringToAddressA = ctypes.windll.ws2_32.WSAStringToAddressA
  21. WSAAddressToStringA = ctypes.windll.ws2_32.WSAAddressToStringA
  22. def inet_pton(address_family, ip_string):
  23. # type: (int, str) -> bytes
  24. addr = SockAddr()
  25. ip_string_bytes = ip_string.encode("ascii")
  26. addr.sa_family = address_family
  27. addr_size = ctypes.c_int(ctypes.sizeof(addr))
  28. try:
  29. attribute, size = {
  30. socket.AF_INET: ("ipv4_addr", 4),
  31. socket.AF_INET6: ("ipv6_addr", 16),
  32. }[address_family]
  33. except KeyError:
  34. raise socket.error("unknown address family")
  35. if (
  36. WSAStringToAddressA(
  37. ip_string_bytes,
  38. address_family,
  39. None,
  40. ctypes.byref(addr),
  41. ctypes.byref(addr_size),
  42. )
  43. != 0
  44. ):
  45. raise socket.error(ctypes.FormatError())
  46. return ctypes.string_at(getattr(addr, attribute), size)