import optparse
import socket

parser = optparse.OptionParser('usage %prog -H <target host> -p <target port>')
parser.add_option('-H',
                  dest='tgtHost',
                  type='string',
                  help='specify target host'
                  )

parser.add_option('-p',
                  dest='tgtPort',
                  type='int',
                  help='specify target port'
                  )

(options, args) = parser.parse_args()

tgtHost = options.tgtHost
tgtPort = options.tgtPort

if tgtHost is None or tgtPort is None:
    print parser.usage
    exit(0)


def connScan(tgtHost, tgtPort):
    try:
        connSkt = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        connSkt.connect((tgtHost, tgtPort))
        print '[+]%d/tcp open' % tgtPort
        connSkt.close()
    except: # fix bare exception!!!!!!!!!!!!
        print '[-]%d/tcp closed' % tgtPort


def portScan(tgtHost, tgtPorts):
    try:
        tgtIP = socket.gethostbyname(tgtHost)
    except: # here too
        print "[-] Cannot resolve '%s': Unknown host" % tgtHost
        return
    try:
        tgtName = socket.gethostbyaddr(tgtIP)
        print '\n[+] Scan results for: %s' % tgtName[0]
    except: # make it stop!
        print '\n[+] Scan results for: %s' % tgtIP
    socket.setdefaulttimeout(1)
    for tgtPort in tgtPorts:
        print 'Scanning port %d' % tgtPort
        connScan(tgtHost, int(tgtPort))

@@ -28,566 +28,8 @@
 et%0A%0A
-parser = optparse.OptionParser('usage %25prog -H %3Ctarget host%3E -p %3Ctarget port%3E')%0Aparser.add_option('-H',%0A                  dest='tgtHost',%0A                  type='string',%0A                  help='specify target host'%0A                  )%0A%0Aparser.add_option('-p',%0A                  dest='tgtPort',%0A                  type='int',%0A                  help='specify target port'%0A                  )%0A%0A(options, args) = parser.parse_args()%0A%0AtgtHost = options.tgtHost%0AtgtPort = options.tgtPort%0A%0Aif tgtHost is None or tgtPort is None:%0A    print parser.usage%0A    exit(0)%0A%0A
 %0Adef
@@ -697,29 +697,27 @@
 esults for: 
-%25s
 ' 
-%25
++
  tgtIP%0A    s
@@ -805,13 +805,11 @@
 ort 
-%25d
 ' 
-%25
++
  tgt
@@ -845,16 +845,744 @@
 , int(tgtPort))%0A
+%0A%0Adef main():%0A    parser = optparse.OptionParser('usage %25prog -H %3Ctarget host%3E -p %3Ctarget port%3E')%0A    parser.add_option('-H',%0A                      dest='tgtHost',%0A                      type='string',%0A                      help='specify target host'%0A                      )%0A    parser.add_option('-p',%0A                      dest='tgtPort',%0A                      type='int',%0A                      help='specify target port'%0A                      )%0A    (options, args) = parser.parse_args()%0A    tgtHost = options.tgtHost%0A    tgtPorts = str(options.tgtPort).split(', ')%0A    if tgtHost is None or tgtPorts%5B0%5D is None:%0A        print parser.usage%0A        exit(0)%0A    portScan(tgtHost, tgtPorts)%0A%0Aif __name__ == '__main__':%0A    main()%0A

Add __name__ == '__main__' block so modules will execute from command line