I've been trying to write a simple Port Scanner using a simple switch function built with if. I've already searched here for a proper solution on how to make the switch function as usable as in C++ or similliar. I found the problem already being discussed on here Replacements for switch statement in Python? and i've tried a few of these approaches but haven't had any luck so far making it run properly. Whatever method i picked from the link i always get a syntax error on the second switch case like this :
File "/home/h4v0kkx0008c/Desktop/Sources/setup/PortScannerV1.py", line 44
if case(2):
^
SyntaxError: invalid syntax
What might be the problem here?
This is the full code i wrote so far :
from socket import *
import time, socket, os, sys, string, argparse, logging, math
n = raw_input("Choose Port range: \n(1)Standard Ports: 0 - 1025\n(2)Middle Range: 1025 - 5000\n(3)Higher Range: 5000 - 49151\n(4)Highest Range: 49151 - 65535\n(5)All Ports: 0 - 65535\n(6)Custom ")
class switch(object):
value = None
def __new__(class_, value):
class_.value = value
return True
def case(*args):
return any((arg == switch.value for arg in args))
while switch(n):
if case(1):
print"Your choice is: Standard Ports"
target= raw_input("Please specify Host or Ip to scan: ")
targetIP = gethostbyname(target)
print 'Starting scan on host ', targetIP
for i in range(0, 1025):
s = socket(AF_INET, SOCK_STREAM)
result = s.connect_ex((targetIP, i)
if case(2):
print"Your choice is: Middle Range"
target= raw_input("Please specify Host or Ip to scan: ")
targetIP = gethostbyname(target)
print 'Starting scan on host ', targetIP
for i in range(1025, 5000):
s = socket(AF_INET, SOCK_STREAM)
result = s.connect_ex((targetIP, i)
break
if case(3):
print"Your choice is: Higher Range"
target= raw_input("Please specify Host or Ip to scan: ")
targetIP = gethostbyname(target)
print 'Starting scan on host ', targetIP
for i in range(500, 49151):
s = socket(AF_INET, SOCK_STREAM)
result = s.connect_ex((targetIP, i)
break
if case(4):
print"Your choice is: Highest Range"
target= raw_input("Please specify Host or Ip to scan: ")
targetIP = gethostbyname(target)
print 'Starting scan on host ', targetIP
for i in range(49151, 65535):
s = socket(AF_INET, SOCK_STREAM)
result = s.connect_ex((targetIP, i)
break
if case(5):
print"Your choice is: All Ports"
target= raw_input("Please specify Host or Ip to scan: ")
targetIP = gethostbyname(target)
print 'Starting scan on host ', targetIP
for i in range(0, 65535):
s = socket(AF_INET, SOCK_STREAM)
result = s.connect_ex((targetIP, i)
break
if case(6):
print"Your choice is: Custom"
target= raw_input("Please specify Host or Ip to scan: ")
targetIP = gethostbyname(target)
print 'Starting scan on host ', targetIP
startPort= raw_input("Specify starting Port:")
endPort= raw_input("Specify end Port:")
for i in range(startPort, endPort):
s = socket(AF_INET, SOCK_STREAM)
result = s.connect_ex((targetIP, i)
break