continue loop after socket error

385
March 18, 2017, at 10:19 AM

I'm using a python loop for finding open ports. The way it should work is if a port from my list is open and accepting a tcp connection, it will send me a response and if not it will skip to the next port number.

The problem I'm having is if the first port isn't responding, I receive an error from socket and the loop stops. Here's the script including how my ports list is defined.

import numpy;
import socket;
import sys;
with open("ports.ls") as f:
    ports = f.read().split(",");
portslist = []
for i in ports:
    portslist.append(i.strip());
portslist = [int(i) for i in portslist]
for i in portslist:
    target_host = sys.argv[1];
    target_port = i;
    print "[*] port:%d" % target_port;
    client = socket.socket(socket.AF_INET, socket.SOCK_STREAM);
    client.connect((target_host, i));
    client.send("GET / HTTP/1.1\r\nHost: %s\r\n\r\n" % target_host);
    response = client.recv(4096);
    print response;

and the error I get is

socket.error: [Errno 11] Resource temporarily unavailable

How do I continue the loop to the next iteration despite the first port not being responsive?

Answer 1

Assuming that was a exception raised in this script and not passed back from the remote server, you can just add an exception handler.

...and get rid of the semicolons!

import numpy
import socket
import sys
with open("ports.ls") as f:
    ports = f.read().split(",")
portslist = []
for i in ports:
    portslist.append(i.strip())
portslist = [int(i) for i in portslist]
for i in portslist:
    target_host = sys.argv[1]
    target_port = i
    print "[*] port:%d" % target_port
    try:
        client = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        client.connect((target_host, i))
        client.send("GET / HTTP/1.1\r\nHost: %s\r\n\r\n" % target_host)
        response = client.recv(4096)
        print response
    except socket.error as e:
        print e
Rent Charter Buses Company
READ ALSO
Extract last two number of IP in ansible

Extract last two number of IP in ansible

I need some help coming up with a very simple way of extracting the last two numbers from an IP address in ansibleMy playbook currently looks like this

415
Out of memory with python IDLE

Out of memory with python IDLE

When I have a relatively large data(~a page of lines), iea long str, very long int, dict, or a list(mainly dicts and lists), and I wanted to check something from the data by using print, my IDLE always run out of memory and starts to not respond and not print anything...

494
Adding Two num­bers rep­re­sented by a linked list, Num­ber Stored in FORWARD order- Python

Adding Two num­bers rep­re­sented by a linked list, Num­ber Stored in FORWARD order- Python

I am new to programming data structuresI am trying to add 2 numbers represented by linked lists stored in FORWARD order

291
Insertion sort error list index

Insertion sort error list index

I am trying to create an array of 7 randomly generated numbers and then sorting these numbers from smallest to highest using the insertion sort methodI have looked through several previously answered topics as it is a very common question but each user has very different...

458