Make find_hostname_and_port its own function

So it can be used to generate multiple ports, which will be needed
to link services.
This commit is contained in:
2021-06-27 00:26:41 +02:00
parent eef07da56a
commit 6458586179
2 changed files with 13 additions and 10 deletions

View File

@ -1,7 +1,8 @@
import datetime
import re
import secrets
from typing import Dict
import socket
from typing import Dict, Tuple
# thanks jess!
IRCV3_FORMAT_STRFTIME = "%Y-%m-%dT%H:%M:%S.%f%z"
@ -15,6 +16,15 @@ def random_name(base: str) -> str:
return base + "-" + secrets.token_hex(8)
def find_hostname_and_port() -> Tuple[str, int]:
"""Find available hostname/port to listen on."""
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.bind(("", 0))
(hostname, port) = s.getsockname()
s.close()
return (hostname, port)
"""
Stolen from supybot:
"""