Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Jul 27th, 2005, 1:30 AM   #1
Riddle
Programmer
 
Riddle's Avatar
 
Join Date: May 2005
Location: Nar Shaddaa
Posts: 42
Rep Power: 0 Riddle is on a distinguished road
Send a message via ICQ to Riddle Send a message via AIM to Riddle Send a message via MSN to Riddle
Very simple Python port scanner.

Well.. a friend asked for help with a Python port scanner he was working on, and I tweaked it and came up with this. I tried commenting the semi-confusing or interesting parts so you socket-noobs can understand it (I say "tried," because I'm not very good at explaining myself. ) Anyway, do you have any ideas as to what I could do to make it better? Thanks in advance.

import socket
target= raw_input("Target: ")
startport= input("Starting port: ")
endport= input("Ending port: ")
for port in range(startport,endport + 1):
#I have '+ 1' because range() ends one number under the second argument.
  scan = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
  scan.settimeout(1)
  if not scan.connect_ex((target, port)):
#connect_ex would return a number higher than zero had it failed, so 'not' is appropriate.
    print "Port",port,"is open."
    scan.close()
Riddle is offline   Reply With Quote
Old Jul 27th, 2005, 5:38 AM   #2
Riddle
Programmer
 
Riddle's Avatar
 
Join Date: May 2005
Location: Nar Shaddaa
Posts: 42
Rep Power: 0 Riddle is on a distinguished road
Send a message via ICQ to Riddle Send a message via AIM to Riddle Send a message via MSN to Riddle
Okay I've updated it, with a logging function.. now it appends all open ports to a file. This is the code to do that:
log= file("scan_log.log","a")
log.write("Open ports for %s:\n" % target)
#the next line should be within the for loop. I had to use %s because write() wanted a string.. and port was (obviously) an int.
log.write("%s\n" % port)
#and this should be outside of the for loop (last line)
log.close()
Riddle is offline   Reply With Quote
Old Jul 28th, 2005, 8:04 PM   #3
Sane
Programming Guru
 
Sane's Avatar
 
Join Date: Apr 2005
Location: Waterloo, Ontario
Posts: 2,028
Rep Power: 6 Sane will become famous soon enough
Send a message via MSN to Sane
Quote:
#the next line should be within the for loop. I had to use %s because write() wanted a string.. and port was (obviously) an int.
log.write("%s\n" % port)
If you wanted it a string, why not:

log.write(str(port)+'\n')

=S


Edit: Oh were you saying you had to use %s as opposed to %d?
Sane is offline   Reply With Quote
Old Jul 28th, 2005, 8:14 PM   #4
Riddle
Programmer
 
Riddle's Avatar
 
Join Date: May 2005
Location: Nar Shaddaa
Posts: 42
Rep Power: 0 Riddle is on a distinguished road
Send a message via ICQ to Riddle Send a message via AIM to Riddle Send a message via MSN to Riddle
I hadn't thought of that.

Yes I was saying I had to use %s instead of %d. Had I been using printf() in C++, I would have used %d... but I'm not sure how much of a difference it makes in Python. Either way, it works.
Riddle is offline   Reply With Quote
Old Jul 29th, 2005, 4:59 PM   #5
Cerulean
Professional Programmer
 
Cerulean's Avatar
 
Join Date: Apr 2005
Location: London, England
Posts: 459
Rep Power: 4 Cerulean is on a distinguished road
Quote:
If you wanted it a string, why not:

log.write(str(port)+'\n')
Bear in mind that constructing strings with the + operator is slower than using string formatting, as it needs to construct intermediate strings as it goes along. Not that it matters in 99.9% of the cases, it's just nice to know if you really need to optimize.

Quote:
Yes I was saying I had to use %s instead of %d. Had I been using printf() in C++, I would have used %d... but I'm not sure how much of a difference it makes in Python. Either way, it works.
You don't need anything other than %s. I don't like using %d in Python as you have to worry about another exception being raised if the item is not a number. Using %s implicitly calls str() on the item. You'll always get pretty formatting of your variable, and not have to waste time remembering other characters - something that tends to interrupt work flow.
Cerulean is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 1:14 PM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC