diff --git a/daemon.py b/daemon.py index 94ce5096..98780ad4 100644 --- a/daemon.py +++ b/daemon.py @@ -147,6 +147,7 @@ from shares import outboxUndoShareUpload from shares import addShare from shares import removeShare from shares import expireShares +from utils import isPortInUse from utils import removeAvatarFromCache from utils import locatePost from utils import getCachedPostFilename @@ -5481,6 +5482,11 @@ def runDaemon(mediaInstance: bool,maxRecentPosts: int, \ pubHandler = partial(PubServerUnitTest) else: pubHandler = partial(PubServer) + + if isPortInUse(proxyPort): + print('Process is already running on port '+str(proxyPort)) + return False + try: httpd = ThreadingHTTPServer(serverAddress, pubHandler) except Exception as e: diff --git a/utils.py b/utils.py index c3aa5720..3b9034db 100644 --- a/utils.py +++ b/utils.py @@ -11,6 +11,13 @@ import time import shutil import datetime import json +import socket + +def isPortInUse(portNumber: int) -> bool: + """Returns true if the port is in use + """ + with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s: + return s.connect_ex(('localhost', portNumber)) == 0 def removeAvatarFromCache(baseDir: str,actorStr: str) -> None: """Removes any existing avatar entries from the cache