Plex plugin to to play various online streams (mostly Latvian).

connection.py 4.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. from __future__ import absolute_import
  2. import socket
  3. try:
  4. from select import poll, POLLIN
  5. except ImportError: # `poll` doesn't exist on OSX and other platforms
  6. poll = False
  7. try:
  8. from select import select
  9. except ImportError: # `select` doesn't exist on AppEngine.
  10. select = False
  11. def is_connection_dropped(conn): # Platform-specific
  12. """
  13. Returns True if the connection is dropped and should be closed.
  14. :param conn:
  15. :class:`httplib.HTTPConnection` object.
  16. Note: For platforms like AppEngine, this will always return ``False`` to
  17. let the platform handle connection recycling transparently for us.
  18. """
  19. sock = getattr(conn, 'sock', False)
  20. if sock is False: # Platform-specific: AppEngine
  21. return False
  22. if sock is None: # Connection already closed (such as by httplib).
  23. return True
  24. if not poll:
  25. if not select: # Platform-specific: AppEngine
  26. return False
  27. try:
  28. return select([sock], [], [], 0.0)[0]
  29. except socket.error:
  30. return True
  31. # This version is better on platforms that support it.
  32. p = poll()
  33. p.register(sock, POLLIN)
  34. for (fno, ev) in p.poll(0.0):
  35. if fno == sock.fileno():
  36. # Either data is buffered (bad), or the connection is dropped.
  37. return True
  38. # This function is copied from socket.py in the Python 2.7 standard
  39. # library test suite. Added to its signature is only `socket_options`.
  40. # One additional modification is that we avoid binding to IPv6 servers
  41. # discovered in DNS if the system doesn't have IPv6 functionality.
  42. def create_connection(address, timeout=socket._GLOBAL_DEFAULT_TIMEOUT,
  43. source_address=None, socket_options=None):
  44. """Connect to *address* and return the socket object.
  45. Convenience function. Connect to *address* (a 2-tuple ``(host,
  46. port)``) and return the socket object. Passing the optional
  47. *timeout* parameter will set the timeout on the socket instance
  48. before attempting to connect. If no *timeout* is supplied, the
  49. global default timeout setting returned by :func:`getdefaulttimeout`
  50. is used. If *source_address* is set it must be a tuple of (host, port)
  51. for the socket to bind as a source address before making the connection.
  52. An host of '' or port 0 tells the OS to use the default.
  53. """
  54. host, port = address
  55. if host.startswith('['):
  56. host = host.strip('[]')
  57. err = None
  58. # Using the value from allowed_gai_family() in the context of getaddrinfo lets
  59. # us select whether to work with IPv4 DNS records, IPv6 records, or both.
  60. # The original create_connection function always returns all records.
  61. family = allowed_gai_family()
  62. for res in socket.getaddrinfo(host, port, family, socket.SOCK_STREAM):
  63. af, socktype, proto, canonname, sa = res
  64. sock = None
  65. try:
  66. sock = socket.socket(af, socktype, proto)
  67. # If provided, set socket level options before connecting.
  68. _set_socket_options(sock, socket_options)
  69. if timeout is not socket._GLOBAL_DEFAULT_TIMEOUT:
  70. sock.settimeout(timeout)
  71. if source_address:
  72. sock.bind(source_address)
  73. sock.connect(sa)
  74. return sock
  75. except socket.error as e:
  76. err = e
  77. if sock is not None:
  78. sock.close()
  79. sock = None
  80. if err is not None:
  81. raise err
  82. raise socket.error("getaddrinfo returns an empty list")
  83. def _set_socket_options(sock, options):
  84. if options is None:
  85. return
  86. for opt in options:
  87. sock.setsockopt(*opt)
  88. def allowed_gai_family():
  89. """This function is designed to work in the context of
  90. getaddrinfo, where family=socket.AF_UNSPEC is the default and
  91. will perform a DNS search for both IPv6 and IPv4 records."""
  92. family = socket.AF_INET
  93. if HAS_IPV6:
  94. family = socket.AF_UNSPEC
  95. return family
  96. def _has_ipv6(host):
  97. """ Returns True if the system can bind an IPv6 address. """
  98. sock = None
  99. has_ipv6 = False
  100. if socket.has_ipv6:
  101. # has_ipv6 returns true if cPython was compiled with IPv6 support.
  102. # It does not tell us if the system has IPv6 support enabled. To
  103. # determine that we must bind to an IPv6 address.
  104. # https://github.com/shazow/urllib3/pull/611
  105. # https://bugs.python.org/issue658327
  106. try:
  107. sock = socket.socket(socket.AF_INET6)
  108. sock.bind((host, 0))
  109. has_ipv6 = True
  110. except Exception:
  111. pass
  112. if sock:
  113. sock.close()
  114. return has_ipv6
  115. HAS_IPV6 = _has_ipv6('::1')