mirror of
https://github.com/explosion/spaCy.git
synced 2025-01-15 12:06:25 +03:00
Use bind to check port instead of connect_ex.
This commit is contained in:
parent
3e8de73b61
commit
5710d8371d
|
@ -102,14 +102,15 @@ def serve(
|
|||
|
||||
# automatically switch to the next available port if the default / given port is taken
|
||||
available_port = port
|
||||
while is_port_in_use(available_port) and available_port <= 65535:
|
||||
|
||||
while is_port_in_use(available_port) and available_port < 65535:
|
||||
available_port += 1
|
||||
|
||||
if is_in_jupyter():
|
||||
warnings.warn(Warnings.W011)
|
||||
render(docs, style=style, page=page, minify=minify, options=options, manual=manual)
|
||||
|
||||
if port > 65535:
|
||||
if available_port == 65535 and is_port_in_use(available_port):
|
||||
raise ValueError(Errors.E1048.format(host=host))
|
||||
|
||||
if available_port != port:
|
||||
|
|
|
@ -958,7 +958,7 @@ class Errors(metaclass=ErrorsWithCodes):
|
|||
E1046 = ("{cls_name} is an abstract class and cannot be instantiated. If you are looking for spaCy's default "
|
||||
"knowledge base, use `InMemoryLookupKB`.")
|
||||
E1047 = ("`find_threshold()` only supports components with a `scorer` attribute.")
|
||||
E1048 = ("No port available for displacy on host {host}. Please specify a port by `displacy.serve(doc, port)`.")
|
||||
E1048 = ("No port available found for displacy on host {host}. Please specify an available port by `displacy.serve(doc, host, port)`.")
|
||||
|
||||
|
||||
# Deprecated model shortcuts, only used in errors and warnings
|
||||
|
|
|
@ -1739,5 +1739,12 @@ def all_equal(iterable):
|
|||
|
||||
|
||||
def is_port_in_use(port):
|
||||
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
|
||||
return s.connect_ex(('localhost', port)) == 0
|
||||
"""Check if localhost:port is in use."""
|
||||
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
|
||||
try:
|
||||
s.bind(("localhost", port))
|
||||
return False
|
||||
except socket.error:
|
||||
return True
|
||||
finally:
|
||||
s.close()
|
Loading…
Reference in New Issue
Block a user