I am taking user input for port number and using that to start a python webserver. If I type in 20000000 as the port number then I get an OverflowError exception (as expected) however; is there any way I can handle this exception within the code so as it will just return an invalid port response whenever this error is spun up ?
from http.server import BaseHTTPRequestHandler, HTTPServer
import time
hostName = "localhost" #self-explanatory - local host
#unit test - error message when ports are over 65,535
valid = False
while not valid: #loop until the user enters a valid int
try:
x = int(input('Welcome to EZ Python Webserver \nEnter the port number for your webserver i.e. 8000')) #Enter port you wish to use
valid = True #if this point is reached, x is a valid int
except ValueError:
print('Please only input digits') #Error message for not inputting digits
except OverflowError as err:
print('Overflowed after ', err)
else:
serverPort = x