0

I am writing a program in Python and a part of it is scanning for active ports on a website. in the module scanports, while if I were to say scan ports 79 to 81, I know that it should return a list with 80 in it. I know this for sure because when I run scanport it shows port 80 is up. Sorry for not having any comments:

import subprocess, socket, urllib2, sys
class pymap:
    def __init__(self):
        pass

################################################################################
################################################################################

    def host(self, host):
        self.host = host
        socket1 = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        self.sock = socket1

################################################################################
################################################################################

    def getip(self):
        if self.host == None:
            print "Specify a host first."
        else:
            return socket.gethostbyname(self.host)

################################################################################
################################################################################

    def scanports(self, start, end):
        ports = []
        self.sock.settimeout(0.000001)
        for i in xrange(start, end+1):  #49151
            try:
                self.sock.connect((self.host, i))
                ports.append(i)
            except:
                pass
        return i

################################################################################
################################################################################

    def scanport(self, port1):
        self.sock.settimeout(0.000001)
        try:
            self.sock.connect((self.host, port1))
            return 1
        except:
            return 0

################################################################################
################################################################################

    def traceroute(self):
        if self.host == None:
            print "Specify a host first."

        else:
            proc=subprocess.Popen(('tracert', self.host), shell=True, stdout=subprocess.PIPE)
            output=proc.communicate()[0]
            return output

################################################################################
################################################################################

    def getsource(self, url):
        page = urllib2.urlopen(url)
        return page.read()

################################################################################
################################################################################

x = pymap()
x.host("www.google.com")
print x.scanports(70, 85)
print x.scanport(80)

EDIT: I changed it, thanks James Henstridge for pointing out that I was using the iteration variable, otherwise it would be much harder. However, it still doesn't work:

    def scanports(self, start, end):
        ports = []
        self.sock.settimeout(3)
        for i in xrange(start, end+1):  #49151
            try:
                self.sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
                self.sock.connect((self.host, port1))
                self.sock.close()
                ports.append(i)
            except:
                pass
            return ports

EDIT: I figured it out, it was a problem with ports.append, thanks for your help.

9
  • Your socket time out is really low. TCP is fast but I'm not so sure you're always going to complete a three way handshake in one microsecond. Commented Oct 11, 2012 at 0:51
  • I tried it at 10 seconds and it returned the same result as it does with 0.000001 seconds. Same result with scanport(). Commented Oct 11, 2012 at 0:57
  • I just noticed you're not closing your socket after you're done. Usually you put that in a finally clause. Give that a try. Commented Oct 11, 2012 at 1:00
  • 3
    @marklap "... finally clause. Give that a try." I see what you did there. Commented Oct 11, 2012 at 1:19
  • 1
    It won't help at your concrete problem, but it will make your code future-proof: instead of self.sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) and self.sock.connect((self.host, port1)), better use self.sock = socket.create_connection((self.host, port1)). Commented Oct 11, 2012 at 5:26

1 Answer 1

2

There are a few issues with your code:

  1. Your scanports method is returning the loop iteration variable, so it will always return end no matter what ports it detects. Perhaps you meant to return the ports list instead?

  2. You are reusing the same socket over and over. From the connect system call man page:

    Generally, connection-based protocol sockets may successfully connect() only once

    So if you want to test multiple connection attempts, create a new socket each time. You should also close those sockets after use.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.