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.

64 line
1.9KB

  1. import os
  2. import sys
  3. __import__('eventlet.green._socket_nodns')
  4. __socket = sys.modules['eventlet.green._socket_nodns']
  5. __all__ = __socket.__all__
  6. __patched__ = __socket.__patched__ + [
  7. 'create_connection',
  8. 'getaddrinfo',
  9. 'gethostbyname',
  10. 'gethostbyname_ex',
  11. 'getnameinfo',
  12. ]
  13. from eventlet.patcher import slurp_properties
  14. slurp_properties(__socket, globals(), srckeys=dir(__socket))
  15. if os.environ.get("EVENTLET_NO_GREENDNS", '').lower() != 'yes':
  16. from eventlet.support import greendns
  17. gethostbyname = greendns.gethostbyname
  18. getaddrinfo = greendns.getaddrinfo
  19. gethostbyname_ex = greendns.gethostbyname_ex
  20. getnameinfo = greendns.getnameinfo
  21. del greendns
  22. def create_connection(address,
  23. timeout=_GLOBAL_DEFAULT_TIMEOUT,
  24. source_address=None):
  25. """Connect to *address* and return the socket object.
  26. Convenience function. Connect to *address* (a 2-tuple ``(host,
  27. port)``) and return the socket object. Passing the optional
  28. *timeout* parameter will set the timeout on the socket instance
  29. before attempting to connect. If no *timeout* is supplied, the
  30. global default timeout setting returned by :func:`getdefaulttimeout`
  31. is used.
  32. """
  33. err = "getaddrinfo returns an empty list"
  34. host, port = address
  35. for res in getaddrinfo(host, port, 0, SOCK_STREAM):
  36. af, socktype, proto, canonname, sa = res
  37. sock = None
  38. try:
  39. sock = socket(af, socktype, proto)
  40. if timeout is not _GLOBAL_DEFAULT_TIMEOUT:
  41. sock.settimeout(timeout)
  42. if source_address:
  43. sock.bind(source_address)
  44. sock.connect(sa)
  45. return sock
  46. except error as e:
  47. err = e
  48. if sock is not None:
  49. sock.close()
  50. if not isinstance(err, error):
  51. err = error(err)
  52. raise err