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

  • Home
  • SEARCH
  • 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 8763945
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T15:54:15+00:00 2026-06-13T15:54:15+00:00

I am trying to create a python script to check if an port is

  • 0

I am trying to create a python script to check if an port is available or not. Below is a piece of the code (not the total script).

But when I run the script the terminal shows no output, when I press ctrl + c I get an single result of the script, when I hit ctrl+c again I get the second result. When the script is done it finally quits…

#!/usr/bin/python

import re
import socket
from itertools import islice

resultslocation = '/tmp/'
f2name = 'positives.txt'
f3name = 'ip_addresses.txt'
f4name = 'common_ports.txt'

#Trim down the positive results to only the IP addresses and scan them with the given ports in the common_ports.txt file

with open(resultslocation + f2name, 'r') as f2, open(resultslocation + f3name, 'w') as f3:
    hits = f2.read()
    list = re.findall(r'name = (.+).', hits)
    for items in list:
        ip_addresses = socket.gethostbyname(items)
        with open(resultslocation + f4name, 'r') as f4:
            for items in f4:
                ports = int(items)
                s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
                try:
                    s.connect((ip_addresses, ports))
                    s.shutdown(2)
                    print 'port', ports, 'on', ip_addresses, 'is open'
                except:
                    print 'port', ports, 'on', ip_addresses, 'is closed'

What am I doing wrong?

Thanks in advance!

  • 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-13T15:54:17+00:00Added an answer on June 13, 2026 at 3:54 pm

    By default, sockets are created in blocking mode.

    So in general it is recommended to call settimeout() before calling connect() or pass a timeout parameter to create_connection() and use that instead of connect. Since you code already captures exceptions, the first option is easy to implement;

    with open(resultslocation + f2name, 'r') as f2, open(resultslocation + f3name, 'w') as f3:
        hits = f2.read()
        list = re.findall(r'name = (.+).', hits)
        for items in list:
            ip_addresses = socket.gethostbyname(items)
            with open(resultslocation + f4name, 'r') as f4:
                for items in f4:
                    ports = int(items)
                    s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
                    s.settimeout(1.0) # Set a timeout (value in seconds).
                    try:
                        s.connect((ip_addresses, ports))
                        s.shutdown(2)
                        print 'port', ports, 'on', ip_addresses, 'is open'
                    except:
                        # This will alse catch the timeout exception.
                        print 'port', ports, 'on', ip_addresses, 'is closed'
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to create a python script which I will later run as
I'm trying to create a Python script that would : Look into the folder
I have a python script that is trying to create a directory tree dynamically
I'm trying to create a GUI Python app using Py2App, but am having trouble
Trying to create a python package. Seems to work, but i get a warning.
I am trying to create a python script that opens a single page at
I'm trying to create a database connection in a python script to my DB2
I'm trying to create a python script that logs into JIRA using their REST
I was trying to create a polling script in python that starts when another
I'm trying to write a Makefile for a python script, that will create a

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.