Github user vanzin commented on a diff in the pull request: https://github.com/apache/spark/pull/22247#discussion_r213400676 --- Diff: python/pyspark/java_gateway.py --- @@ -147,6 +147,39 @@ def do_server_auth(conn, auth_secret): raise Exception("Unexpected reply from iterator server.") +def local_connect_and_auth(port, auth_secret): + """ + Connect to local host, authenticate with it, and return a (sockfile,sock) for that connection. + Handles IPV4 & IPV6, does some error handling. + :param port + :param auth_secret + :return: a tuple with (sockfile, sock) + """ + sock = None + errors = [] + # Support for both IPv4 and IPv6. + # On most of IPv6-ready systems, IPv6 will take precedence. + for res in socket.getaddrinfo("127.0.0.1", port, socket.AF_UNSPEC, socket.SOCK_STREAM): + af, socktype, proto, _, sa = res + sock = socket.socket(af, socktype, proto) + try: + sock.settimeout(15) + sock.connect(sa) + except socket.error as e: + emsg = _exception_message(e) + errors.append("tried to connect to %s, but an error occured: %s" % (sa, emsg)) + sock.close() + sock = None + continue + break --- End diff -- Slight shorter (and more "python-compliant"?): - move the socket initialization (and the return) inside the try - get rid of the continue - use an else instead of the condition below
--- --------------------------------------------------------------------- To unsubscribe, e-mail: reviews-unsubscr...@spark.apache.org For additional commands, e-mail: reviews-h...@spark.apache.org