Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8655835
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T15:08:08+00:00 2026-06-12T15:08:08+00:00

I am writing a program in Python and a part of it is scanning

  • 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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T15:08:10+00:00Added an answer on June 12, 2026 at 3:08 pm

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm writing a Python program that, if the user changes settings while running it,
Writing a python program, and I came up with this error while using the
I'm writing a python program that uses scons to build an .exe and then
I am writing a python program that lauches a subprocess (using Popen). I am
I am writing a basic program in Python that prompts the user to enter
I'm writing python program to build mac-address cache using pcap. But pcap module for
I'm writing a program in python that uses genetic techniques to optimize expressions. Constructing
I have a problem: I am writing a program in Python 3.2 that requires
I'm writing a Python module that wraps out a certain web service API. It's
I'm writing a Python program with a GUI built with the Tkinter module. I'm

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.