def port_to_tcp(port=None):
<EOL>domain_name = socket.getfqdn()<EOL>try:<EOL><INDENT>addr_list = socket.getaddrinfo(domain_name, None)<EOL><DEDENT>except Exception:<EOL><INDENT>addr_list = socket.getaddrinfo('<STR_LIT:127.0.0.1>', None)<EOL><DEDENT>family, socktype, proto, canonname, sockaddr = addr_list[<NUM_LIT:0>]<EOL>host = convert_ipv6(sockaddr[<NUM_LIT:0>])<EOL>address =  '<STR_LIT>' + host<EOL>if port is None:<EOL><INDENT>port = ()<EOL><DEDENT>if not isinstance(port, int):<EOL><INDENT>context = zmq.Context()<EOL>try:<EOL><INDENT>socket_ = context.socket(zmq.REP)<EOL>socket_.ipv6 = is_ipv6(address)<EOL>port = socket_.bind_to_random_port(address, *port)<EOL><DEDENT>except Exception:<EOL><INDENT>print('<STR_LIT>'.format(address, addr_list))<EOL>pypet_root_logger = logging.getLogger('<STR_LIT>')<EOL>pypet_root_logger.exception('<STR_LIT>'.format(address))<EOL>raise<EOL><DEDENT>socket_.close()<EOL>context.term()<EOL><DEDENT>return address + '<STR_LIT::>' + str(port)<EOL>
Returns local tcp address for a given `port`, automatic port if `None`