util.py 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. import socket
  2. import warnings
  3. def g_log(*args):
  4. warnings.warn("eventlet.util.g_log is deprecated because "
  5. "we're pretty sure no one uses it. "
  6. "Send mail to eventletdev@lists.secondlife.com "
  7. "if you are actually using it.",
  8. DeprecationWarning, stacklevel=2)
  9. import sys
  10. from eventlet.support import greenlets as greenlet
  11. g_id = id(greenlet.getcurrent())
  12. if g_id is None:
  13. if greenlet.getcurrent().parent is None:
  14. ident = 'greenlet-main'
  15. else:
  16. g_id = id(greenlet.getcurrent())
  17. if g_id < 0:
  18. g_id += 1 + ((sys.maxint + 1) << 1)
  19. ident = '%08X' % (g_id,)
  20. else:
  21. ident = 'greenlet-%d' % (g_id,)
  22. print >>sys.stderr, '[%s] %s' % (ident, ' '.join(map(str, args)))
  23. __original_socket__ = socket.socket
  24. def tcp_socket():
  25. warnings.warn("eventlet.util.tcp_socket is deprecated."
  26. "Please use the standard socket technique for this instead:"
  27. "sock = socket.socket()",
  28. DeprecationWarning, stacklevel=2)
  29. s = __original_socket__(socket.AF_INET, socket.SOCK_STREAM)
  30. return s
  31. try:
  32. # if ssl is available, use eventlet.green.ssl for our ssl implementation
  33. from eventlet.green import ssl
  34. def wrap_ssl(sock, certificate=None, private_key=None, server_side=False):
  35. return ssl.wrap_socket(sock,
  36. keyfile=private_key, certfile=certificate,
  37. server_side=server_side, cert_reqs=ssl.CERT_NONE,
  38. ssl_version=ssl.PROTOCOL_SSLv23, ca_certs=None,
  39. do_handshake_on_connect=True,
  40. suppress_ragged_eofs=True)
  41. except ImportError:
  42. # if ssl is not available, use PyOpenSSL
  43. def wrap_ssl(sock, certificate=None, private_key=None, server_side=False):
  44. try:
  45. from eventlet.green.OpenSSL import SSL
  46. except ImportError:
  47. raise ImportError("To use SSL with Eventlet, "
  48. "you must install PyOpenSSL or use Python 2.6 or later.")
  49. context = SSL.Context(SSL.SSLv23_METHOD)
  50. if certificate is not None:
  51. context.use_certificate_file(certificate)
  52. if private_key is not None:
  53. context.use_privatekey_file(private_key)
  54. context.set_verify(SSL.VERIFY_NONE, lambda *x: True)
  55. connection = SSL.Connection(context, sock)
  56. if server_side:
  57. connection.set_accept_state()
  58. else:
  59. connection.set_connect_state()
  60. return connection
  61. def wrap_socket_with_coroutine_socket(use_thread_pool=None):
  62. warnings.warn("eventlet.util.wrap_socket_with_coroutine_socket() is now "
  63. "eventlet.patcher.monkey_patch(all=False, socket=True)",
  64. DeprecationWarning, stacklevel=2)
  65. from eventlet import patcher
  66. patcher.monkey_patch(all=False, socket=True)
  67. def wrap_pipes_with_coroutine_pipes():
  68. warnings.warn("eventlet.util.wrap_pipes_with_coroutine_pipes() is now "
  69. "eventlet.patcher.monkey_patch(all=False, os=True)",
  70. DeprecationWarning, stacklevel=2)
  71. from eventlet import patcher
  72. patcher.monkey_patch(all=False, os=True)
  73. def wrap_select_with_coroutine_select():
  74. warnings.warn("eventlet.util.wrap_select_with_coroutine_select() is now "
  75. "eventlet.patcher.monkey_patch(all=False, select=True)",
  76. DeprecationWarning, stacklevel=2)
  77. from eventlet import patcher
  78. patcher.monkey_patch(all=False, select=True)
  79. def wrap_threading_local_with_coro_local():
  80. """
  81. monkey patch ``threading.local`` with something that is greenlet aware.
  82. Since greenlets cannot cross threads, so this should be semantically
  83. identical to ``threadlocal.local``
  84. """
  85. warnings.warn("eventlet.util.wrap_threading_local_with_coro_local() is now "
  86. "eventlet.patcher.monkey_patch(all=False, thread=True) -- though"
  87. "note that more than just _local is patched now.",
  88. DeprecationWarning, stacklevel=2)
  89. from eventlet import patcher
  90. patcher.monkey_patch(all=False, thread=True)
  91. def socket_bind_and_listen(descriptor, addr=('', 0), backlog=50):
  92. warnings.warn("eventlet.util.socket_bind_and_listen is deprecated."
  93. "Please use the standard socket methodology for this instead:"
  94. "sock.setsockopt(socket.SOL_SOCKET,socket.SO_REUSEADDR, 1)"
  95. "sock.bind(addr)"
  96. "sock.listen(backlog)",
  97. DeprecationWarning, stacklevel=2)
  98. set_reuse_addr(descriptor)
  99. descriptor.bind(addr)
  100. descriptor.listen(backlog)
  101. return descriptor
  102. def set_reuse_addr(descriptor):
  103. warnings.warn("eventlet.util.set_reuse_addr is deprecated."
  104. "Please use the standard socket methodology for this instead:"
  105. "sock.setsockopt(socket.SOL_SOCKET,socket.SO_REUSEADDR, 1)",
  106. DeprecationWarning, stacklevel=2)
  107. try:
  108. descriptor.setsockopt(
  109. socket.SOL_SOCKET,
  110. socket.SO_REUSEADDR,
  111. descriptor.getsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR) | 1)
  112. except socket.error:
  113. pass