issue with this program and sockets

issue with this program and sockets

Postby Comp_guymn » Tue Sep 17, 2013 6:46 am

Hey guy, IM n00b at python and im trying to follow this book to compile a tcp port scanner. im using idle in 2.7.5 and I tried writing my own but in the process I wanted to test out the actually coding in the book and it coming up with errors, the coding seems logical correct after comparing with socket module itself so im not sure why, please throw some feedback , thankyou

from socket import *
def connScan(tgtHost, tgtPort):
connSkt = socket(AF_INET, SOCK_STREAM) # <-----------INVAILD SNYTAX ERROR
connSkt.connect((tgtHost, tgtPort))
results = connSkt.recv(100)
print '[+]%d/tcp open'% tgtPort
print '[+] ' + str(results)
except :
print '[-]%d/tcp closed'% tgtPort
def portScan(tgtHost, tgtPorts):
try :
tgtIP = gethostbyname(tgtHost)
except :
print "[-] Cannot resolve '%s': Unknown host" %tgtHost
try :
tgtName = gethostbyaddr(tgtIP)
print '\n[+] Scan Results for: ' + tgtName[0]
except :
print '\n[+] Scan Results for: ' + tgtIP
for tgtPort in tgtPorts:
print 'Scanning port ' + tgtPort
connScan(tgtHost, int(tgtPort))
def main():
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='string', \
help='specify target port[s] separated by comma')
(options, args) = parser.parse_args()
tgtHost = options.tgtHost
tgtPorts = str(options.tgtPort).split(', ')
if (tgtHost == None) | (tgtPorts[0] == None):
print '[-] You must specify a target host and port[s].'
portScan(tgtHost, tgtPorts)
if __name__ == '__main__':
Last edited by Yoriz on Tue Sep 17, 2013 4:34 pm, edited 1 time in total.
Reason: First post lock
Posts: 3
Joined: Tue Sep 17, 2013 6:40 am

Re: issue with this program and sockets

Postby Yoriz » Tue Sep 17, 2013 4:37 pm

Hi, welcome to the forum.
Indentation is important in python code, you code needs to be posted inside code tags to preserve the indentation.
Please read the 'new users read this' link in my signature for how you should post your code and errors.
New Users, Read This
Join the #python-forum IRC channel on!
User avatar
Posts: 1634
Joined: Fri Feb 08, 2013 1:35 am
Location: UK

Re: issue with this program and sockets

Postby micseydel » Tue Sep 17, 2013 5:32 pm

Get rid of the open parenthesis following the colon for your try.
Join the #python-forum IRC channel on for off-topic chat!

Please prefer not to PM members. The point of the forum is so that anyone can benefit. We don't want to help you over PMs/emails/Skype chats that others can't benefit from :)
User avatar
Posts: 2919
Joined: Tue Feb 12, 2013 2:18 am
Location: Mountain View, CA

Return to Networking

Who is online

Users browsing this forum: No registered users and 2 guests