Check if port is already used

main
Bob Mottram 2020-02-19 11:37:33 +00:00
parent 8ac9a43b29
commit 0277b309d9
2 changed files with 13 additions and 0 deletions

View File

@ -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:

View File

@ -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